Sunday, July 12, 2009

HELP!! Lost on homework C++ (dogsafire)?

dogsafire,


I haven't got far. We just got this and didn't get much info. I am in a C++ programing class and the teacher just reads from power point. Last semester only two people passed, now I see why.


I have the highest score in the class and stll don't know what the heck I am doing till we are about 3 weeks past it.





This is our first assn in arrays, we are using Walter Savitch's 6th Edition. I am just trying to get started on this prob. I am having a hard time understanding how to translate the prob into:


const int MAX = 10;


void outputA (int a[], int size);


int main () {


int a[MAX];


int i;


int x;


for (i=0;i%26lt;MAX;i++)


a[i]=0;


cout %26lt;%26lt; "Input grades ending with -1 "; // Has to be an int for this situation like(-1)


cin %26gt;%26gt; x;


i = 0;


while (x%26gt;=0 %26amp;%26amp; i%26lt;MAX){


a[i] = x;


i++;


cout %26lt;%26lt; "next: ";


cin %26gt;%26gt; x;}


for (i=0;i%26lt;MAX;i++)


cout %26lt;%26lt; a[i] %26lt;%26lt; endl;return 0;


outputA(a,i);


}


void outputA (int a[], int size){


int i;


for (i=0;i%26lt;size;i++)


cout %26lt;%26lt; a[i] %26lt;%26lt; endl;


}

HELP!! Lost on homework C++ (dogsafire)?
If you are still stuck with your homework may be you can post it at homework help website like http://homeworkhelp.co.in/ .
Reply:Your code isn't bad, you don't appear to be lost. My comments are inlined below. Is there something in particular you're confused about?





const int MAX = 10;





// void outputA (int a[], int size);


// recommend const pointer, const data for int[] arg


void outputA(const int * const, int);





int main () {


int a[MAX] = {0}; // see comment below


int i;


int x;





#if 0


for (i=0;i%26lt;MAX;i++) // don't need this...


a[i]=0; // init on declaration (above)


#endif





cout %26lt;%26lt; "Input grades ending with -1: ";


cin %26gt;%26gt; x;


i = 0;


#if 0


while (x%26gt;=0 %26amp;%26amp; i%26lt;MAX){


a[i] = x;


i++;


cout %26lt;%26lt; "next: ";


cin %26gt;%26gt; x;


}


#else


// do something like this so you don't


// get the prompt for grade MAX+1


while (x %26gt;= 0) {


a[i++] = x;


if (i == MAX) break;


cout %26lt;%26lt; "next: ";


cin %26gt;%26gt; x;


}


#endif





#if 0


for (i=0;i%26lt;MAX;i++) // after this loop i=10, so


cout %26lt;%26lt; a[i] %26lt;%26lt; endl; // the call to outputA will


// return 0; ??? // print it all again


#endif





outputA(a,i);


}





// void outputA (int a[], int size){


void outputA(const int * const a, int size){


int i;


for (i=0;i%26lt;size;i++)


cout %26lt;%26lt; a[i] %26lt;%26lt; endl;


}

survey monkey

No comments:

Post a Comment