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
I am struggling to get this program to work. Could someone please look it over to see what is wrong? (The compiler says that sum_positive is not defined in this way.) Also, I have searched this problem and found an answer on here, but I did not see the problem that way and want to avoid plagiarism when I turn in my homework. Thank you!
Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.
Program:
#include <iostream>
using namespace std;
int main()
{
int number, n = 10, sum_positive = 0, sum_negative = 0, sum = 0;
int count = 0, positive_count = 0, negative_count = 0;
cout << "Please enter in 10 whole numbers, one at a time." << endl;
do
{
cout << "Please enter another number." << endl;
cin >> number;
n--;
}
while (n > 0);
if (number >= 0)
{
sum_positive += number;
//positive_count++; count++;
}
else
{
sum_negative += number;
//negative_count++; count++;
}
sum = sum_positive + sum_negative;
cout << sum_postive << endl;
cout << sum_negative << endl;
cout << sum << endl;
return 0;
}
there is a spelling mistake in your code of sum_positive in line (cout << sum_postive << endl;) - you have written sum_postive but you declared sum_positive.
you are running a loop and taking input in the same variable number, so it will overwrite all the inputs and will store only the last value in variable number which user entered. So your sum will always be equal to the last value user entered.
For this you need to use an array.
Example:
int number[10], n=10, sum_positive=0;
do{
cout<<"e`enter code here`nter number:";
cin>>number[n];
n--;
}while(n>0);
now for sum also you need to use an loop.
If you do not know about an array, study about it how to use it.
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 2 years ago.
Improve this question
I have this really simple program, with a for loop designed to run 3 times.
For every iteration, it will ask the user to enter in the weather. Each time, the user's input is stored in an integer array called C.
Once the loop concludes, I have another for loop that print's out the user's input. This next loop works fine for the 2 values, but gives off some weird messed up value once it reaches the third iteration.
int main(){
//Variable declaration:
int days;
int C[2];
int F[2];
for(days=0; days<3; days++){
cout << "What is the temperature in celsius for day " << days + 1 << ":" << endl;
cin >> C[days];
F[days] = (C[days] * 9/5) + 32;
}
cout << "\nCelsius\t\t Farenheit\n-------\t\t ---------" << endl;
for(days =0; days <3; days++){
cout << C[days] << "\t\t " << F[days] << endl;
}
return 0;
}
This next loop works fine for the 2 values but gives off some weird messed up value once it reaches the third iteration.
Your definition of C states it has 2 entries, but your loop runs for
three iterations (0, 1, and 2).
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 4 years ago.
Improve this question
I am tasked with creating a program that outputs the first N prime numbers. The user inputs the value N. Here is my current program. It may look weirdly formatted but I don't know how to fix that. It's pretty easy to see what is happening though.
#include <iostream>
using namespace std;
int main() {
cout << "How many prime numbers?";
int N;
cin >> N;
if(N=1){
cout << "2";
}
if(N>1){
cout << "2" << "\n";
int i=N-1; //i=prime counters
int j=3; //j=test prime
do {
int k;
for(k=2; k<j;){ //divisibility test
if(j%k!=0 and k!=j-1){ //indivisible, check next divisor
k=k+1;
continue;
}
if(j%k!=0 and k==j-1){ //indivisible and last divisor, display prime
cout << j << "\n";
i=i-1 //removes a prime counter
break;
}
if(j%k==0){ // divisible, break from loop
break;
}
}
j=j+1; //test next prime
} while(i>0); //will not continue printing primes if number has exceeded N
}
return 0;
}
The program outputs "2" no matter what. Then, it tests each number for divisibility with every number before it except for 1. If it is indivisible and still has divisors to get through, it continues. If it is indivisible and has no more divisors left, it is printed, and one "prime counter" is taken away. If it is divisible, the loop is broken and it moves on to test the next number. When the prime counters reach 0, the do while loop ends and the program is complete.
I reached an issue with the program when it outputted the following:
How many prime numbers?
2
I have no idea why this is happening since I specified N as an integer and asked for the user to input N. It doesn't even give me the opportunity to input N, it just automatically prints "2". What is going on?
You had a few basic errors in your code. You can see a working version at
http://coliru.stacked-crooked.com/a/3e6ab22ca6e15b00
The main error you had was
if(N=1){
cout << "2";
}
you meant to write
if(N==1){
cout << "2";
}
Note the equality checking is with == and assignment is with =. Any decent compiler will tell you you are using it wrong. For example you should see a warning on your compiler output like
main.cpp: In function 'int main()':
main.cpp:8:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(N=1){
~^~
But in general the code works.
#include <iostream>
using namespace std;
int main() {
int N = 10;
if(N=1){
cout << "2";
}
if(N>1){
cout << "2" << "\n";
int i=N-1; //i=prime counters
int j=3; //j=test prime
do {
int k;
for(k=2; k<j;){ //divisibility test
if(j%k!=0 and k!=j-1){ //indivisible, check next divisor
k=k+1;
continue;
}
if(j%k!=0 and k==j-1){ //indivisible and last divisor, display prime
cout << j << "\n";
i=i-1; //removes a prime counter
break;
}
if(j%k==0){ // divisible, break from loop
break;
}
}
j=j+1; //test next prime
} while(i>0); //will not continue printing primes if number has exceeded N
}
return 0;
}
and the output is
2
3
5
7
11
13
17
19
23
29
You may also want to change your code to remove the
using namespace std;
It is generally considered bad practise. More info here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int i,t,x[20], even, odd, prime;
cout << "Enter 20 integer numbers from 0 to 99: "<<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}
cout << "\nPrime numbers are: " << endl ;
prime=1;
for (i=2; i<=20 ; i++)
{
for(t=2;t<x[i];t++)
{
if(x[i]%t==0)
{
prime=0;
}
}
if(prime==1)
{
cout << x[i] << endl;
}
prime=1;
}
for(i=1; i<=20; i++) // this is where i have problem.
{
if(x[i]% 2 == 0)
{
even++;
}
else
{
odd++;
}
}
cout << "Number of odd numbers: " << odd << "\n";
cout << "Number of even numbers: " << even << "\n";
return 0 ;
}
When i compile it shows even (40) and odd (10) for input of 0 till 19. Where it should show even 10(including the 0) and odd (10). Im not sure where am i doing it wrongly. I hope someone can help me improve the code.
Variables even and odd are never set to a known value, so you are not formally allowed to read from them. Doing so invokes that most infamous Standardese concept: undefined behaviour. So the values of these variables could be right or could be wrong; the variables and all code trying to read them could be optimised entirely out of your program; or anything can happen. You cannot rely on these variables doing anything right. All attempts to read them make your program ill-formed, so now it can do anything, including things you would never have imagined.
You should search for the abundant background info about these concepts, but I like to think I made a fairly decent summary here: https://stackoverflow.com/a/38150162/2757035
Also, as Thomas points out in the comments, you appear not to understand how array indexing works: Indexes are 0-based. So, int i[20] declares 20 elements numbered from 0 to 19. You try to access index 20, which is not part of the array and hence is more undefined behaviour.
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 years ago.
Improve this question
#include <iostream>
#include <cmath>
using namespace std;
bool prime(int n);
int main()
{
double i;
while (true)
{
cout << "Enter a number that isn't 0: ";
cin >> i;
if ( i == 0)
break;
if(prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime." << endl;
}
system ("Pause");
return 0;
}
bool prime (int n)
{
int i;
double sqrt_of_n = sqrt(double (n));
for (i = 2; i <= sqrt_of_n; i++)
{
if (int(n) % 1 == 0)
return false;
}
return true;
}
Everytime I run the program, if I input 7, I get that 7 isn't prime. Can someone help me figure out where I messed up?
I have tried changing between double and int for i and n.
If I input 3, it shows prime.
The problem is that it's showing some prime numbers as not prime.
The body of your for loop doesn't use i at all.
In particular, n % 1 is always zero, for any integral n.
Presumably you want to know whether n is divisible by i, but accidentally checked if n is divisible by 1.
You could easily have discovered this mistake yourself by single-stepping in a debugger, and making the various subexpressions into "watch expressions".
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 8 years ago.
Improve this question
I'm almost positive it's something simple, but i cannot for the life of me figure it out. This whole code is to print a menu which asks users for the and array size, then fills it with random numbers, sorts it ascending and descending, printing the array, as well as letting the user use a binary search or a sequential search. I know a linear search is much more efficient for what we're supposed to do, but the instructor insists on a binary search. I have the Binary search working, and it prints out the correct result, but with a 1 on the end of it(i.e position 14 comes out as 141). here's the switch case that calls the function:
case 7:
int num, result;
cout << "Please enter an int to search for" << endl;
cin >> num;
result = binarySearch(Array1, num, 0, size);
cout << num << "was found at position " << result;
break;
}
}
here is the function:
int binarySearch(int arr[], int key, int first, int last)
{
while (first <= last)
{
int mid = (last + first) / 2;
if (key < arr[mid])
{
last = mid - 1;
}
else if (key > arr[mid])
{
first = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
Just tried the code on my machine - I am not getting an extra 1 appended to the output. So, I would think the 1 is being printed out somewhere else.
First, add in the space (or endl at the end of cout. This will confirm that the result you're getting is correct and also that the 1 is being printed elsewhere. Then, you can try looking for the extra 1 being printed in your code (after the switch() probably).
cout << num << "was found at position " << result << " ";
Update the rest of the code if you need further help.