for loop has me confused with local variable

/ change the contents of an array using a function

#include <iostream>
using namespace std;

void cube(int *n, int num);

int main()
{
int i, nums[10];

for(i = 0; i < 10; i++) nums[i] = i + 1;


cout << "Original contents: " << endl << endl;

for(i = 0; i < 10; i++) cout << nums[i] << " ";


cout << endl << endl;

cube(nums, 10); // compute cubes



cout << "Altered contents: ";
for(i = 0; i < 10; i++) cout << nums[i] << " ";
cout << endl << endl;

system("pause");
return 0;
}

void cube(int *n, int num)
{
while(num)
{
*n = *n * *n * *n;
num--;
n++;
}
}




ok first guys let me get this out, this is not homework, I am a hurt fat, middle aged guy learning c++ on my own. now to the problem. I am lost on the for loops. I understand the first for loop is loading the index of the array and the end assignment statement is adding 1 so the arrray is indexed 1-10 instead of 0-9. I get confused on the 2nd for loop.....after the cout << "Original contents"; statement. To me it looks like this for loop would go with the local scope of i in that for statement and print the index of the array 0-9 instead of 1-10, unless since the index has already been loaded it will ouput what is loaded already...hmm a practice program has been thought of.
Anyways all comments good, bad or nasty are appreciated.

Dan
First - code tags ;)
[code] [\code]


Next - indentation. It's standard to either use braces (i prefer)
or at least indent unbraced single statements of for/while loops etc.
It's much easier to see the scope of variables and you'll be less likely to make mistakes.

Finally, about the for loops. The braces show the scope of the variables in the loops. You can certainly declare i where you did and each loop that it gets to it's still a valid iterator for the loop. At each loop you reassign the value of i to 0 so there's no noticeable difference. Overall though, it is usually better to keep local variables local and it won't cost any extra noticeable performance. Each loop that you have starts i back at 0 (not at 1) if it got to 10 you would be trying to access a part of the array that isn't there - ie: your program (hopefully) crashing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

void cube(int *n, int num);

int main()
{
int i, nums[10];

for(i = 0; i < 10; i++) 
{
   nums[i] = i + 1;
}

cout << "Original contents: " << endl << endl;

for(i = 0; i < 10; i++) 
{
   cout << nums[i] << " ";
}


cout << endl << endl;

cube(nums, 10); // compute cubes


cout << "Altered contents: ";
for(i = 0; i < 10; i++) 
{
   cout << nums[i] << " ";
}
cout << endl << endl;

system("pause");
return 0;
}

void cube(int *n, int num)
{
   while(num)
   {
      *n = *n * *n * *n;
      num--;
      n++;
   }
}

Last edited on
hey guys I got it figured out...and yes I am a stickler for indentation just need to get used to formatting on here. I just didn't realize the array held it's own values I should say...

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{

int myArray[10];

for(int x = 0; x < 9; x++) myArray[x] = rand();

cout << "original contents: " << endl;

for(int i = 0; i < 9; i++) cout << myArray[i] << endl;

system("pause");
return 0;
}




that snip of code sure doesn't print out an index of 0-9 LOL...Thanks again...learning on your own stinks at times. All comments positive negative or nasty are welcome.

Dan
You'll want to seed rand() or else you'll get the same sequence of numbers each time the program runs.
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

This is preferred for the loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for(int i = 0; i < 10; i++) //start with a common outer index like 'i'
{
   myArray[i] = rand();
   for(j = 5; j < 10; j++)   //1st inner loop indexed with 'j'. 'i' is in scope
   {
      myArray[j] += myArray[j] - 1000 * i;
      for(k = 0; k < 5; k++) //2nd inner loop indexed with k. j and i are still in scope
      {
          myArray[k] += myArray[k] % (i + j); //% is the modulus operator
      }
   }
}
cout << "original contents: " << endl;

//outer 'for' loop index with i again. If there were an inner loop it would be called j and k again
// j and k and the i from the other loops are not in scope 
for(int i = 0; i < 10; i++) 
   cout << myArray[i] << endl;


Note that int myArray[10]; has exactly 10 elements in it. Namely
myArray[0],myArray[1],myArray[2],myArray[3],myArray[4],myArray[5],
myArray[6],myArray[7],myArray[8],myArray[9]
But this loop only places a value in the first 9 elements...
ie: myArray[9] is not being used
for(int x = 0; x < 9; x++) myArray[x] = rand();
Last edited on
Soranz,

Thank you very much,
I had often heard you must seed for random number and now I have some code to play with it. Thank you again for your time and patience.

Dan
Topic archived. No new replies allowed.