Saturday, May 22, 2010

Easy Basic C++ Programming Help?

1) I can't figure out how to get the oddnumber count of my scores


2) I can't figure out how to get the highestscore


3) Is my while loop correct to repeat the program w/o typing 'x'?





#include %26lt;iostream%26gt;





using namespace std;





int main()


{





int counter = 1;


int totalodd = 0;


int num1;


int scores;


char letter;


int numbers;


int highestscore = 0;





cout%26lt;%26lt;"How many scores are you entering?"%26lt;%26lt;endl;


cin%26gt;%26gt;num1;





for (int counter=1; counter%26lt;=num1; counter++)


{


cout%26lt;%26lt;"Enter an integer: "%26lt;%26lt;endl;





cin%26gt;%26gt;scores;


}





totalodd = ( ( scores % 2) != 0);








cout%26lt;%26lt;"You entered "%26lt;%26lt;totalodd%26lt;%26lt;" odd numbers."%26lt;%26lt;endl;





if ( scores %26gt; highestscore )





highestscore = scores;








cout%26lt;%26lt;"The highest # is "%26lt;%26lt;highestscore%26lt;%26lt;endl;








cout%26lt;%26lt;"Finish...press 'x' to exit program. any other key to run again."%26lt;%26lt;endl;





while (letter != 'x')

Easy Basic C++ Programming Help?
There are a number of problems with this code. Here are a few tips to get you going:





cin %26gt;%26gt; scores;





You only get the last number entered. You need to collect them all:





After num1 is entered, declare and initialize scores to give you room for num1 integers:





int *scores = new int[num1];





Then : cin %26gt;%26gt; scores[counter]


in your loop to save each entered score.





Loop from counter = 0, whille counter %26lt; num1, instead of 1 through num1 as you have now.





In that same loop you can keep track of the highest score entered, and also increment totalodd for each odd number entered. Similar to what you have, except it'll look like:


if (scores[counter] %26gt; highestscore) ...


and using scores[counter] for your odd check.





By the way, another way to check for oddness is:


(scores[counter] %26amp; 1)


If true, i.e. == 1, it's odd.





Remember to do this:


delete [ ] scores;


when you're done with it.





I guess you didn't include all of your code, because


while (letter != 'x')


by itself isn't doing anything. You need a matching 'do' for the beginning of the loop. You also left out: cin %26gt;%26gt; letter; to get letter.





Keep working, you'll get it.





EDIT: someone else asked a similar question (both working on the same assignment?), and I realized you don't need to collect the entered scores for this simple problem. For future reference, though, the tips on allocating and using the int array are still good.
Reply:this is what i understood so far:





#include%26lt;iostream.h%26gt;


void main()


{


int score,total_odd=0,max_score;


cout%26lt;%26lt;"How many scores are you entering?\n";


cin%26gt;%26gt;score;


max_score=score;


for(int i=1;i%26lt;=score;i+=2)


{


total_odd++;


if(score%26gt;max_score)


max_score=score;


}


cout%26lt;%26lt;"you've entered"%26lt;%26lt;total_odd%26lt;%26lt;"odd numbers\n";


cout%26lt;%26lt;"The highest score is"%26lt;%26lt;max_score;


}
Reply:You're repeatedly reading integers into scores, but you never use scores. Each read wipes out what was there previously. You have to examine each score one by one to count the odd values.





Hope that helps.
Reply:Tip #1) you need to make count = 0. Otherwise, you'll be one short when you use your "for loop" to get the number of scores. (the compiler doesn't count "1" as it's first number, so you really it would count like this: 2,3,4,5,6...num1) It needs to be able to count from 0 to 1 in order to include 1 as the number.





Second, your test for the highest score isn't inside of a loop, so it'll only do that test once. And then it'll reinitialize all the variables at the end, wiping out your info anyway. I suggest doing all your testing inside your "for loop". Also, if you're looking to find the total amount of odd numbers entered, you need an "odd number count" integer to keep track of that.


No comments:

Post a Comment