Sunday, July 12, 2009

Help with C++ code.?

can any1 help me figure the code out to this question?


I'm stuck. Make it in c++.





The Question:


Write a function allValid that takes an array of integer grades and the size of the array as parameters. The function returns the index of the first value that is negative or over 100. If this is not true of any of the values, they are all valid, and the function returns the size of the array.


Write a driver to test this function. A driver in this context is code that controls other code. A test driver is a main() that calls the other code to test if it produces the correct result.





My code so far:


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





void allValid(int, int);





int main()


{


int grades[] = {100, 90, 85, 76, 71};


const int DATA_SIZE = 5;





for(int x = 0; x %26lt; DATA_SIZE; x++)


}


void allValid(int grades[], int size[])


{


for(int x = 0; x %26lt; 0 %26amp;%26amp; x %26gt; 100; x = invalid)


if(x %26gt;= 0 || x %26lt;= 100);


}





Please try to help.





Thanks in advance.

Help with C++ code.?
You have a number of problems:





1. You never call your allValid subroutine in main().





2. You don't understand the for(;;) construct. Until you get some more experience with it, limit yourself the the vanilla :


for(%26lt;initialize the loop counter%26gt;; %26lt;condition necessary to do another pass%26gt;, %26lt;increment counter%26gt;)





3. A for statement executes the next statement after the for.


If you have multiple lines of code, you need to make a compound statement by enclosing those lines in braces.


If fact get in the habit of using braces every time you write a for, while or do-while loop. It makes it more obvious to code readers what is going on.


Braces are inexpensive. I usually buy them in bulk lots of 50kg.





4. You can't return a value if the return value of your subroutine is void. Actually you can, but that's beyond what you're attempting to do here.





5. Most programmers reserve i,j,k,l,m,n for loop counters. It's something that goes back to the days of FORTRAN





6. It's a good practice to start your loops at 0 and set the cut off at N where N is the size of your array. That way your counter will always be a valid index into the array.





Here is what I came up with:








#include %26lt;iostream%26gt;


#include %26lt;string%26gt;








int allValid(int[], int);





int main()


{


int grades[] = {100, 90, 85, 76, 71};


const int DATA_SIZE = 5;





int returnValue = allValid(grades, DATA_SIZE);





return 0;


}











int allValid(int grades[], int size)


{


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


{


if(grades[i] %26lt; 0 || 100 %26lt; grades[i])


{


return grades[i];


}


}





//Fall through if all are OK





return size;





}








One other thing you can do:





const int DATA_SIZE = sizeof(grades)/sizeof(int);





That way if you add to the array of grades, you don't have to count them to get DATA_SIZE.





sizeof(grades) gives you the number of bytes in the array grades[], sizeof(int) gives you the number of bytes in int.


They are computed by the compiler so they can be used to set the value of a const variable.


No comments:

Post a Comment