Minimize Number- c++ [closed] - c++

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 2 years ago.
Improve this question
how can I Print the maximum possible number of operations that can be performed?
The operation is as follows: if all numbers are even then divide each of them by 2 otherwise, you can not perform any more operations.
First line contains a number N (1 ≤ N ≤ 200) number of elements.
Second line contains N numbers (1  ≤  A I  ≤  109).
Examples
Input
3
8 12 40
Output
2
Input
4
5 6 8 10
Output
0
I will write the cod in the comment cause I don't know how to put it here ..can you please edit it for me?
I know my code is wrong but I can't fix it so please explain it for me .

In this loop:
for(int i=0; i<n && 2<=n<=100 ;i++) {
2<=n<=100 is not doing what you mean. It evaluates to (2 <= n) <= 100) which is a different check.
You need to do:
2 <= n && n <= 100
You need to fix the other checks where you are doing this as well.
Also, since n is not being modified inside the loop, you can hoist this check out of the loop:
if (2<=n && n <=100) {
for(int i=0; i<n ;i++) {
and similarly for the other loops as well.

You are taking 'x' with n and m, but it has to be at end.
Also just use an 'index' variable to keep track of where to insert in your array. And then use the same 'index' while finding the 'x'.
#include <iostream>
using namespace std;
int main()
{
int a[100000],n,m,x;
cin>>n>>m;
bool flag=0;
int index =0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++) {
cin >> a[index];
index++;
}
}
cin >>x;
for(int i=0;i<index;i++){
if(x==a[i]) {
flag = true;
}
}
if(flag==0){
cout<<"will not take number";
} else if(flag==1){
cout<<"will take number";
}
return 0;
}

Related

my bool function not working,dont know why [closed]

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 last month.
Improve this question
My intention was to count the sum of prime numbers in a certain range. Its working fine when the range is less than or equal to 8. When I take input 9 it shows output 26 while the answer is 17. My program identifying 9 as a prime and adding with the sum.
{
for(int i=3; i*i<=n; i+=2)
{
if(n%i==0) return false;
}
return true;
}`
`int main(){
int t,sum=0;
cin >> t;
for(int k = 0; k < t; k++){
int n;
cin >> n;
if(n>=2) sum=2;`
` for(int i=3;i<=n;i+=2)
{
ifPrime(i);
if(ifPrime) sum=sum+i;
}`
` cout<<sum<<endl;
sum=0;
}
return 0;
}
my guess
this
ifPrime(i);
if(ifPrime) sum=sum+i;
should be
if(ifPrime(i)) sum=sum+i;
but without seeing all the code its impossible to say

C++ - Printing prime numbers till N [closed]

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 4 years ago.
Improve this question
Question says:
You are given an integer N. You need to print the series of all prime numbers till N.
I want to know what's wrong with my code and suggestions will also be of great help.
#include<iostream>
using namespace std;
int main()
{
int N;
cin >> N;
int u;
for(int i = N; i > 0; i--)
{
u = 0;
for(int j = 2; j < N-1; j++)
{
if(i % j == 0)
{
u = 1;
}
}
if(u == 0)
{
cout << i << " ";
}
}
return 0;
}
First for future reference you should probably post that on code review, unless there is a specific problem then you should create a Minimal, Complete, and Verifiable post.
There is nothing inherently wrong other than you do not check that N>0 which could lead to an infinite loop, j should be going to i not N, and I think this would print 1 as a prime number which it is not. Here are some pointers:
Why are you going from N to 0? That seems a little counter intuitive compared from going from 2 to N (you can skip 1 since it is not prime)
If you are going to use a flag (u) then you should make it a bool which forces it to be either true or false
You do not need to do a flag, instead as soon as you find a divisor print the number and then break the inner loop like
for(j=2; j< N-1; j++){
if(i%j==0){
cout << i << " ";
break;
}
}
You do not need to have j go all the way to i, just the sqrt(i) since anything greater then the sqrt(i) that divides i must must be multiplied by some number smaller then the sqrt(i). So if i is not prime, then there must be a divisor below sqrt(i)

Program runs in compiler but returns runtime error in online judge [closed]

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 2 years ago.
Improve this question
I'm trying to solve this problem on this online judge: https://a2oj.com/ladder?ID=3 (see problem below) using the following code. It runs successfully on the compiler but returns a runtime error on the online judge.
EDIT: Code after changing loop conditions
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct count {
int number;
int repetitions;
};
bool sortByNumber(const struct count &lhs, const struct count &rhs) {
return lhs.number < rhs.number;
}
int main() {
vector <int> input;
int n = 0;
do {
cin>>n;
input.push_back(n);
} while (n != 0);
struct count x[101] = {NULL};
for (int j = 0; j < input.size(); j++) {
int tracker = 0;
for (int z = 0; z < input.size(); z++) {
if (input[j] != x[z].number) {
tracker++;
}
}
if (tracker == input.size()) {
x[j].number = input[j];
}
}
sort(x, x+101, sortByNumber);
for (int y = 0; y < 101; y++) {
for (int w = 0; w < input.size(); w++) {
if (x[y].number == input[w]) {
x[y].repetitions++;
}
}
}
for (int v = 0; v < 101; v++) {
if (x[v].number != 0) {
cout << x[v].number << " " << x[v].repetitions << endl;
}
}
return 0;
}
I'm fairly new to programming so I apologize if the answer is obvious and I can't see it. I've researched causes of runtime errors and I can't see any memory leaks, logic errors, or divisions by zero. The only thing I can think of is that it's a segmentation fault caused by the many nested loops (this code uses a lot more memory and running time than the other programs I submitted to the online judge), but I can't think of another way to solve this problem. Any ideas, even just about where to look, would be very much appreciated.
EDIT: Problem
Problem Statement:
Amgad got a job as a Cashier in a big store, where he gets thousands of dollars everyday. As a cashier, he must count the amount of each dollar bill (banknote) he has at the end of each day.
Amgad wants you to help him by writing a computer program so Amgad can just enter the amount of each bill and you count each bill separately.
Input Format:
one or more positive numbers ending by zero each number is between 1 and 100 inclusive
Output Format:
print each number only once in one line followed by the number of repetitions
Sample Input:
100
20
5
2
10
20
5
5
20
100
10
2
2
10
5
0
Sample Output:
2 3
5 4
10 3
20 3
100 2
As #Component10 mentioned, your array is of a fixed size. Add an integer called count that increments every time a new number is popped out of the input. Change all the integer literal references to 8 to counter.
If input contains more than 101 elements, the conditions
if (tracker == 8) {
x[j].number = input[j];
}
and
if (input[j] != x[z].number) {
tracker++;
}
Are invoking undefined behavior for any j value above 100, due to out-of-bounds access of array x elements. Both z, and j are looped to input.size(), which can be higher than 101.
Undefined behavior can manifest itself in many ways. Runtime error is one of those possibilities.

program which check perfect numbers - something gets wrong [closed]

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 8 years ago.
Improve this question
I have to write a function which gets a number, and prints all the perfect numbers that are smaller than the number given. If there are not any, the function prints an appropriate message.
The program I have just written is compiled well, but it's working only partially. If the input number is 5, for instance, for some reason I don't get any message that there are not perfect numbers in this range (=until 5) .
Can someone please explain to me what's wrong in this program?
I would appreciate any help!
#include<iostream>
using namespace std;
void check (int num, int & j);
void main()
{
int num,j, count;
cout << "List all the perfect numbers less than: ";
cin >> num;
check (num, j);
}
void check (int num, int & j)
{
int i,sum, count=0;
for(j=2;j<num;num++)
{
sum=0;
for(i=1;i<j;i++)
{
if(j%i == 0)
sum += i;
}
if(sum == j)
{cout << j <<endl;
count+=1;
}
}
if (count==0)
cout<<"there are no perfect numbers"<<endl;
}
There is an infinite loop in your code:
for(j=2;j<num;num++)
I think it should be
for(j=2;j<num;j++)

Hint: Missed newline ? while trying to solve the below quiz [closed]

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 8 years ago.
Improve this question
Problem
This is similar to the previous problem, however there will be multiple integers in the input. You have to write a computer program to read each integer and print Even if the integer is divisible by 2, else print Odd. To help further, the number of integers (T) to read will be the first input to the computer program.
Input Format:
First line of input contains count of integers: T. T>=1
After that, each line contains the integer N.
Sample Input:
2
4
5
Sample Output:
Even
Odd
Note: There should be a newline after each output. Otherwise you might end up printing EvenOdd here, which will result in wrong answer.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0) cout<<"Even"<<endl;
else cout<<"Odd"<<endl;
}
return 0;
}
You don't need neither array nor vectors for this:
int main() {
int n;
cin >> n;
while(n--) {
int number;
cin >> number;
// do something with number
}
return 0;
}
Note that this structure is the most common to solve this kind of problems
your problem will be solved if you change
for(int i =0; i <sizeof(a)/sizeof(int); i++)
to
for(int i =1; i <sizeof(a)/sizeof(int); i++)
But, how'd you know the number of elemnts for the array beforehand?
To correct this, you don't declare the array statically. Take the first input, and allocate memory for that many ints.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0)
cout<<"Even"<<endl;
else
cout<<"Odd"<<endl;
}
return 0;
}
this code which u posted is working perfectly bro :)