Ask your own question, for FREE!
Computer Science 52 Online
OpenStudy (anonymous):

Hey everyone, I'm looking for some help with a couple of problems in a code I've been working on! Any help would be greatly appreciated! The first thing is that when it goes to report the min/max/etc. stuff (this will make sense in the picture), in a situation where I don't enter any odd numbers (or even), nothing should be reported, but now all I'm getting is an entry of 0 in that situation, and I've really no clue how to fix that. The second issue is that whenever I go to actually enter the integers, regardless of how many I tell it to use, it always stops when I get to 1 before that. Cont..

OpenStudy (anonymous):

So for example when I say I want to enter 4 integers, it will stop me and move on after only 3, and it's always like that. Attached is an image of what it should look like (though I mentioned what I need to do about there being no odd/even numbers). And here is the code itself: // Assignment 2.cpp : main project file. #include <StdAfx.h> #include <iostream> #include <string> using namespace std; int main(void) { //Declaring int min=0,max=0,sumo=0,sume=0,n,j=0,totalnum=0,ne=0,no=0; //Creating a cout statement so the user can input the number of integers the program will compile cout << "How many integers do you want to enter > "; cin >> totalnum; if(totalnum<=0)//This is to verify that a positive integer was entered { cout <<"Please enter a number at least greater than 1 "; cin>> totalnum; } else { do { cout << "Enter integer #"<< j++ << " "; cin >> n; if(j==0) { min=max=n; j+1; } else { if(n>max) { max=n; } if(n<min) { min=n; } } if(n%2==0) { sume+=n; ne++; } else { sumo+=n; no++; } }while(j+1<totalnum); } cout << "Max is " << max << "\n"; cout << "Min is " << min << "\n"; cout << "Number of odds is " << no << "\n"; cout << "Number of evens is " << ne << "\n"; cout << "Sum of odds is " << sumo << "\n"; cout << "Sum of evens is " << sume << "\n"; cout << "Integer average is " << (sume +sumo)/totalnum << "\n"; return system("pause"); }

OpenStudy (anonymous):

1. The way you've stuctured your code, you will always have some kind of output at the end (printint out the max, min, sum, etc.). Also, if you enter 0 for totalnum, you get a division by zero error (since the bottom code always executes and you are comptuing the mean using totalnum). 2. At the start of the program code, you initialize j to 0. At the beginning of the do-while loop, you increment j (it goes from [1,2,3] if totalnum = 4), so if totalnum = 4, j+1 will go from [2,3,4] before breaking out of the loop; it will only execute 3 times instead of the expected 4 :( Number 2 is fixed if you post-increment j within the while() condition at the end: http://ideone.com/Oud8w To solve number 1, use logical strucutres in your code (if statements, loops, etc) to check the input; if you don't want the final part of your code to execute if no (# of odd numbers is 0) or if a bad totalnum was input, then you can use something like if (no != 0 && ne != 0 && totalnum > 0) // if there is no bad input, output the final result { // output stuff } http://ideone.com/20Ixg

OpenStudy (jagatuba):

First, wouldn't it be easier to use a for loop to enter your integers rather than incrementing a counter and performing a check? Example (in pseudo code). for c = 1 to totalnum; print "Enter integer # " c; input n

OpenStudy (anonymous):

@jagatuba I thought the do {} while () part was already doing that :-P

OpenStudy (anonymous):

ahh.... right.

OpenStudy (anonymous):

the for loop is indeed better to use: http://ideone.com/gb13h

OpenStudy (jagatuba):

Yes, but it doesn't seem that efficient to me, but I'm not a C expert like yourself. Also why not use an int array to store the entries. That way all entries are entered and then calculations can occur afer the entry loop.

OpenStudy (jagatuba):

Yes I would prefer the for loop in this instance.

OpenStudy (jagatuba):

Also, why only positive integers? the screenshot show both positive and negative integers.

OpenStudy (jagatuba):

Slight alteration to some of your code ag. http://ideone.com/vpWzt

OpenStudy (anonymous):

right; only totalnum needs to be positive

OpenStudy (jagatuba):

Just formatting. Change it so it asks for #1 #2 etc instead of #0 #1 etc and made Max prin to next line.

OpenStudy (jagatuba):

Oh right DUH.

OpenStudy (jagatuba):

Also where is min and max of odd and even; and average of od and even?

OpenStudy (jagatuba):

This is closer to the output of the screen shot: http://ideone.com/aaXrk

OpenStudy (jagatuba):

Remove min, max, and integer average display and it will be identical to the screen shot.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
DonaldTrumpofQC: Happy St. Patty's Day
3 hours ago 3 Replies 0 Medals
gelphielvr: (BIOLOGY 1) Carbon & Nitrogen (question in the replies)
9 hours ago 1 Reply 0 Medals
jmweeden: how do you draw realistically?
12 hours ago 0 Replies 0 Medals
jmweeden: if you guys had 1 million$ would you spend or save? why?
12 hours ago 0 Replies 0 Medals
jmweeden: how do you jump rope?
12 hours ago 6 Replies 0 Medals
Shrade3: Chemisty please, annoying
21 hours ago 4 Replies 0 Medals
girlypop: how do I save money more?
13 hours ago 18 Replies 4 Medals
girlypop: how do I find perimeter for a square. I'm actually clueless -_-
13 hours ago 5 Replies 4 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!