Thursday, July 30, 2009

In need of C++ programming help please!!?

I have been taking a programming course for a few weeks now and need a little help with an assignment. My assignment is to write a program that displays a menu with the the option to find the largest # with a known quantity of numbers; find the smallest # with an unknown quantity of numbers or quit.





Here is what I have so far, but my book has gotten me so confused. I need some pointers as to what I am missing or what is wrong and needs fixed. Any help would be greatly appreciated.


Serious answers only please.





#include %26lt;iostream%26gt;


using namespace std;


const int SENTINEL = -99; //to end option 'B'


int number; //variable to store numbers


int counter;


char limit; //variable to store amount of numbers to input in 'A'





int main()


{


char sel;


number = 0;


int count = 0;





cout %26lt;%26lt; "A) Find the largest # in a list." %26lt;%26lt; endl;


cout %26lt;%26lt; "B) Find the smallest # in a list." %26lt;%26lt; endl;


cout %26lt;%26lt; "C) Quit." %26lt;%26lt; endl;


cout %26lt;%26lt; "What do you want to do?" %26lt;%26lt; endl;


cin %26gt;%26gt; sel;


switch (sel)

In need of C++ programming help please!!?
You would be better off going to www.homework.com or to www.studybuddy.com There is a lot of help there.
Reply:You're overwriting the previously entered number every time you cin a number from the list.





You need to set up an array, say


int list[100];





Then use an integer variable to count your position in the array


int i;


cout %26lt;%26lt; "Enter the numbers for the list: "


while(i=0;i %26lt; listsize;i++)


cin %26gt;%26gt; list[i];





The better way to do this is to use a linked list since there is then no upper bound on the size of the list of numbers, but that is probably a bit too high level for an early programming class. I'd stick with the array solution.
Reply:well, first, your program will only run through 1 selection then end. you need to make a loop with your selections in it. something like:


do


{


//your selections


//your code


}


while (sel != 'c' || sel != 'C');





next, in your "case 'A'" you never initialize counter or increment it. did you mean to use count instead?





in your "case 'B'" you never increment count, and all it does is read in numbers. I assume you just didnt finish it yet because your still trying to get case a to work first.





thats for starters just off the top of my head
Reply:#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


using namespace std;


int main(){


int i,min_val,max_val;


int list[10];


for(i=0;i%26lt;10;i++) list[i]=rand();


min_val=list[0];


for(i=1;i%26lt;10;i++)


if(min_val%26gt;list[i]) min_val=list[i]


cout%26lt;%26lt;"minimun value: "%26lt;%26lt;min_val%26lt;%26lt;"\n";


max_val=list[0];


for(i=1;i%26lt;10;i++)


if(max_val%26lt;lsit[i])


max_val=list[i];


cout%26lt;%26lt;maximum value: "%26lt;%26lt;max_val%26lt;%26lt;"\n";


return 0;


}


No comments:

Post a Comment