Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int ma(float array[], int N)
{
int k = 0;
float max = array[k];
for (int i = 0; i < N; ++i) {
if (array[i] > max) {
max = array[i];
k = i;
}
}
return k;
}
int main()
{
int t;
while (t--) {
int n;
cin >> n;
int w[n], p[n];
for (int i = 0; i < n; i++)
cin >> w[i];
for (int i = 0; i < n; i++)
cin >> p[i];
float x[n];
for (int i = 0; i < n; i++)
x[i] = p[i] / w[i];
int weigth = 0, profit = 0;
while (weigth <= 20) {
// int k=distance(x, max_element(x, x + n));
// int k= std::distance(x, max_element(x, x + sizeof(x)/sizeof(x)));
int k = ma(x, n);
weigth = weigth + w[k];
profit = profit + p[k];
x[k] = p[k] = w[k] = 0;
}
cout << weigth << endl
<< profit << endl;
}
}
The above code is not printing anything. If you want the question refer to "catch-the-match":
your code is not even compiling,
you can not do this in C++
int n;
cin >> n;
int w[n], p[n];
because n must be a constant at compiling time, on the other hand doing this:
int t;
while (t--) {
is producing an unpredictable number of iterations in the loop since t is not initialized
You have to declare int n as a const int n in order for you code to compile correctly.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I am new to C++ and I am trying to make a program to simply count even numbers and display it to the user, but I am getting the wrong output.
#include <iostream>
using namespace std;
int main()
{
int par = 0;
int tam;
int arr [ ] = {};
cin >> tam;
for (int i = 0; i < tam; i++ ){
cin >> arr[i];
}
for (int i = 0; i < tam; i++ ){
cout << arr[i];
}
for (int i = 0; i < tam; i++){
if (arr[i]%2 == 0) {
par++;
}
cout << par;
return 0;
}
}
Your code exhibits undefined behavior, as you are creating an empty array and then trying to write values to it outside of its bounds. Since you don't know at compile-time how many numbers the user will be entering at runtime, use a std::vector instead of an array.
Also, you have the cout << par; and return 0; statements in the wrong place. You have them inside the final loop, when they should be after the loop instead.
Try something more like this:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int par = 0;
int tam, num;
vector<int> vec;
cin >> tam;
for (int i = 0; i < tam; ++i){
cin >> num;
vec.push_back(num);
}
for (int i = 0; i < tam; ++i){
cout << vec[i];
}
for (int i = 0; i < tam; ++i){
if (vec[i] % 2 == 0) {
++par;
}
}
cout << par;
return 0;
}
You could then eliminate the last loop entirely by using the standard std::count_if() algorithm instead, eg:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int tam, num;
vector<int> vec;
cin >> tam;
for (int i = 0; i < tam; ++i){
cin >> num;
vec.push_back(num);
}
for (int i = 0; i < tam; ++i){
cout << vec[i];
}
cout << count_if(vec.begin(), vec.end(),
[](int num){ return num % 2 == 0; }
);
return 0;
}
Although, you don't actually need the array/vector at all, you can just count values as the user is entering them, eg:
#include <iostream>
using namespace std;
int main()
{
int par = 0;
int tam, num;
cin >> tam;
for (int i = 0; i < tam; ++i){
cin >> num;
if (num % 2 == 0) {
++par;
}
}
cout << par;
return 0;
}
Your are missing a closing curly brace to end the last loop. You will loop only one time through it, show the output and return 0. You need to change to:
for(int i = 0; i < tam; i++)
{
if(arr[i] % 2 == 0)
{
par++;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Here is my code.How can I print out all the even vectors different from 0? The result is incorrect but I don't know what is the problem here.
#include<iostream>
#include<math.h>
using namespace std ;
int main ()
{
int i,j,N,M;
int a[100][100];
cout << "the number of rows of the matrix = " ;
cin >> N;
cout << "the number of columns of the matrix ";
cin >> M;
for (i=1;i<=N;i++){
for(j=1;j<=M;j++)
{
cin >> a[i][j];
}
}
cout <<"even vectors different from 0 ";
for (i=1; i<=N;i++){
for(j=1;j<=M;j++)
{
if (a[i][j]!=0 && a[i][j]%2==0)
{
cout<<a[i][j];
}
}
return 0;
}
}
First of all, int a[100][100]; is very dangerous. If M or N is larger than 100 (or in your case, because you are using 1-based index, 99), it will cause out-of-bounds, which leads to undefined behavior. You should use std::vector<std::vector<int>> instead.
Second, I format the code for you, and the problem becomes clear that return 0; is at the wrong place.
Overall, your code should be:
#include <iostream>
// #include <cmath> why?
#include <vector>
int main ()
{
int N{};
int M{};
std::cout << "the number of rows of the matrix = " ;
std::cin >> N;
std::cout << "the number of columns of the matrix ";
std::cin >> M;
std::vector<std::vector<int>> a(N, std::vector<int>(M));
for (int i = 0;i < N; i++)
{
for(int j = 0;j < M; j++)
{
std::cin >> a[i][j];
}
}
std::cout <<"even vectors different from 0 ";
for (int i = 0; i < N; i++)
{
for(int j = 0;j < M; j++)
{
if (a[i][j] != 0 && a[i][j] % 2 == 0)
{
std::cout << a[i][j];
}
}
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Im solving this question but cannot get correct output it show segmentation fault
https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/agitated-chandan/description/
This is the code that i write for this program
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int visited[100001], dist[100001];
int largest;
int t, x, y, w, i, z, k;
int large;
cin >> t;
while (t--)
{
int large = 0;
int n;
cin >> n;
vector<pair<int, int>> adj[n];
for (i = 1; i < n; i++)
{
cin >> x >> y >> w;
adj[x].push_back(make_pair(y, w));
adj[y].push_back(make_pair(x, w));
}
queue<int> q;
for (k = 1; k <= n; k++)
{
q.push(k);
for (i = 1; i <= n; i++)
{
visited[i] = -1;
dist[i] = 0;
}
largest = 0;
while (!q.empty())
{
z = q.front();
visited[z] = 1;
q.pop();
for (i = 0; i < adj[z].size(); i++)
{
if (visited[adj[z][i].first] == -1)
{
dist[adj[z][i].first] = dist[z] + adj[z][i].second;
if (largest < dist[adj[z][i].first])
{
largest = dist[adj[z][i].first];
}
q.push(adj[z][i].first);
visited[adj[z][i].first] = 1;
}
}
}
if (large < largest)
{
large = largest;
}
}
if (large < 100)
{
cout << 0 << " ";
}
if (large > 100 && large < 1000)
{
cout << 100 << " ";
}
if (large > 1000 && large < 10000)
{
cout << 1000 << " ";
}
if (large > 10000)
{
cout << 10000 << " ";
}
cout << large;
}
}
I expect output be
0 8
Just heck the logic is it write or wrong
One possible cause is that you store indexes from 1 to n to the queue q:
for (k = 1; k <= n; k++) {
q.push(k);
and then use them (as z) for indexing adj of size n:
while (!q.empty()) {
z = q.front(); // z equals n in the first iteration here
...
for (i = 0; i < adj[z].size(); i++) // adj[n] will be accessed
Indexes in C++ are zero-based, therefore the valid ones are from 0 to n-1.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to perform quick sort on a given array in decreasing order but the first number output is always some arbitrary number like 456752.
I am unable to identify the source of problem. Please help
#include <iostream>
#include <cstdlib>
using namespace std;
long randPartition(long a[],long start,long end0)
{
long pivot = start + rand()%(end0 - start);
//swap a[end] with [pivot]
long temp = a[end0];
a[end0] = a[pivot];
a[pivot] = temp;
//now partitioning
long i = start;
for(int j = start; j < end0 ; j++)
{
if(a[j] > a[end0])
{
long temp1 = a[j];
a[j] = a[i];
a[i] = a[temp1];
i++;
}
}
//swapping pivot with its correct position
long temp2 = a[end0];
a[end0] = a[i];
a[i] = temp2;
return i;
}
void quickSort(long ar[],long start,long end0)
{
if (start < end0)
{
long i = randPartition(ar,start,end0);
quickSort(ar,start,i-1);
quickSort(ar,i+1,end0);
}
}
int main()
{
int testCases;
cin >> testCases;
while(testCases--)
{
long size0;
cin >> size0;
long arr[size0];
for(int i = 0; i < size0; i++)
{
cin >> arr[i];
}
//cin >> endl;
//using quick sort algo
quickSort(arr, 0, size0 - 1);
//printing the sorted array
for(int j = 0; j < size0; j++)
{
cout << arr[j] << " ";
}
cout << endl;
}
return 0;
}
Inside randomPartition() At the line with
a[i] = a[temp1];
replace it with
a[i] = temp1;
just a booboo :P
quickSort(arr, 0, size0 - 1); and j < end0 exclude your last value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have written a code to find the largest palindrome formed by multiplication of two 3-digit numbers. However instead of just getting the desired answer i.e. the largest palindrome, I am getting the list of all possible palindromes. How do I program it to find the largest.
Code:
#include <iostream>
using namespace std;
int revfunc(int x) {
int rev = 0, num, d;
num = x;
while(num != 0) {
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
if(x == rev && maxi < x) {
maxi=x;
cout<<maxi<<endl;
}
}
int main() {
long int ans;
for(int i = 100; i <= 999; i++) {
for(int j = 100; j <= 999; j++) {
ans = i * j;
revfunc(ans);
}
}
cin.get();
return 0;
}
In your program you don't actually select the maximum palindrome, you just dump them all. Here is minimal correction for code to work:
bool revfunc(int x){
int rev = 0, num, d;
num = x;
while (num != 0){
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
return x == rev&&maxi < x;
}
int main()
{
int max_palindrome = 0;
long int ans;
for (int i = 100; i <= 999; i++){
for (int j = 100; j <= 999; j++){
ans = i*j;
if (ans > max_palindrome && revfunc(ans))
{
max_palindrome = ans;
}
}
}
cout << max_palindrome;
cin.get();
return 0;
}