Trying a new Code. Do yall believe this is a good code.

Just want to get some comments on this program. Do yall think it will work or what could i improve, so it can display the swaps within the loop.



int main()
{ const int size = 5;
int tests[size] = {87, 75, 98, 100, 82};
int results;

int index = 0; //Used as a subscript to search array
int position = -1; //Used to record postion of search value
bool found = false; //Flag to indicate if the vaule was found

while (index < size && !found)
{
if(tests[index] == tests[size]) //If value is found
{
found = true; //Set the flag
position = index; //Reocrd the value's subscript
}
if( position == -1)
cout<< " You did not earn 100 points on any tests.
"<<endl;
else
{
cout<< " You earned 100 points on the test. "<<endl;
cout<< (position + 1)<< ".
";
}
index++; //Go to next element
}

return 0;
}

Comments

  • if(tests[index] == tests[size])

    the above will get you into problems right out of the gate. size has been declared as a constant equal to 5; however, arrays are 0 based, so valid indexes would be 0-4. tests[5] is an out of bounds error.

    are you looping through tests to see if any = 100?

    [code]
    found = false;

    while(index < size && found == false) {
    if(tests[index] == 100) {
    found = true;
    position = index;
    }

    ++index;
    }

    if(found == true) {
    std::cout << "You earned 100 on test " << (position + 1)
    << std::endl;
    } else {
    std::cout << "You didn't earn 100 on any test"
    << std::endl;
    }[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion