I am going over some of my quizzes with questions I did not get right and was wondering if someone could help me w\ a few of them...

What will the following code print to the screen:

int num[15] = {3,2,8,7,1,5,6};
for (i=0; i <15; i+=3)
{
cout << num[i] << " ";
}

The answer I put for this is 6, 5, 11, 10, 4, 8, 9

Can someone not just explain this to me but kinda show me how to work it so I know it for my final

One more question on another quiz

Use the following code to answer the questions

int main()
{
      ofstream outp;
      outp.open("File1.txt");
      if (outp.fail())
      {
            cout << "Error\n";
            exit(1);
       }
       cout << "Type a negative to exit\n";
       cout << "Enter a number: ";
       cin >> num;
       while (num >= 0)
       {
            outp << num * num << endl;
            cout << "Enter a number: ";
            cin >> num;
       }
       outp.close();
       return 0;
}

the two questions that I got wrong from this example for this quiz were::

Write the statement to declare an object being used to read from a fill. Call the object fin...
I put fin.open("File1.txt");

Write the statement to read in an integer from a file and store the value read in into the variable num1. Use the object your created in #5 (from the precious example)

I put cout << setw(5) << num1 << endl
cout.setf(ios::fixed);

Can anyone explain to me my errors so I dont do it wrong for the final

1) 3 2 8 7 1 5 6 0 0 0 0 0 0 0 0 The reason for all those 0s at the end is that the compiler will initialize any unspecified array elements with 0 as long as there is at least one initializer

Example: int array[15] = {0} is often used to initialize all elements to 0 even though only the first is specified.

I have no idea were you got your answer :)

Your problem with the rest of the questions was you did not read the questions very carefully.

>>Write the statement to declare an object being used to read from a fill
You are to declare the object, not call the open statement. So the correct answer is ifstream fin; >>Write the statement to read in an integer
You wrote a statement to OUTPUT the integer, not read it. The correct answer is fin >> num1;

6, 5, 11, 10, 4, 8, 9

each number in that sequence is 3 more than each number in the following sequence

3, 2, 8, 7, 1, 5, 6

which means you increased the value of each element in num[] by 3 not the index used to access the values in num. Here's how I'd have answered it:

loop    value of    value of
num        i             num[i]
1            0             3
2            3              7
3            6             6

but now thinks would have gotten funky for me. I didn't know that elements of num with indexes 7 through 14 respectively would be initialized to 0. I thought they would have been junk. So if they are truly 0 as in Ancient Dragon's post, and I believe him, then the rest of the table will be

4     9     0
5    12    0

I guess I didn't read it very well either. I didn't notice in the first problem the loop counter was increased by 3 on each loop iteration. So I flunked that question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.