Did I Code This Correctly?

I am trying to write a program that declares an array (alpha) of 50 components of type double. The first 25 components should be equal to the square of the index variable and the last 25 components should be equal to three times the index variable. 10 elements should be printed per line. Did I do this correctly?

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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	double alpha[50];
	int i;

	for (i = 0; i < 25; i++)
	{
		alpha[i] = i^2;				//sets first 25 components of array equal to the square of the index
	}

	for (i = 0; i > 24; i++)
	{
		alpha[i] = i*3;				//sets last 25 components of array equal to three times the index
	}

	for (i = 0; i < 50; i++)																												//for every array component under 50,
	{
	cout << alpha[i] << alpha[i] << alpha[i] << alpha[i] << alpha[i] << alpha[i] << alpha[i] << alpha[i] << alpha[i] << alpha[i] << endl;	//outputs 10 elements per line
	}

	system("pause");																				//keeps program open
}
Last edited on
1st for loop: set indices 0-24 to be index squared.
Good

2nd for loop: set indices 0-23 to be index * 3.
Not good, overwrites 0-23, want this to apply from 25-49.

3rd for loop: Outputs each element 10 times.
Not good, want to print each 1 time, and place an endl every tenth.
like this?
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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	double alpha[50];
	int i, x;
	x = 25;

	for (i = 0; i < 25; i++)
	{
		alpha[i] = i^2;				//sets first 25 components of array equal to the square of the index
	}

	for (i = 0; i >= 25; i++)
	{
		alpha[x] = i*3;				//sets last 25 components of array equal to three times the index
		x = x++;
	}

	for (i = 0; i < 50; i++)																												//for every array component under 50,
	{
	cout << alpha[0] << alpha[1] << alpha[2] << alpha[3] << alpha[4] << alpha[5] << alpha[6] << alpha[7] << alpha[8] << alpha[9] << endl;	//outputs 10 elements per line
	cout << alpha[10] << alpha[11] << alpha[12] << alpha[13] << alpha[14] << alpha[15] << alpha[16] << alpha[17] << alpha[18] << alpha[19] << endl;
	cout << alpha[20] << alpha[21] << alpha[22] << alpha[23] << alpha[24] << alpha[25] << alpha[26] << alpha[27] << alpha[28] << alpha[29] << endl;
	cout << alpha[30] << alpha[31] << alpha[32] << alpha[33] << alpha[34] << alpha[35] << alpha[36] << alpha[37] << alpha[38] << alpha[39] << endl;
	cout << alpha[40] << alpha[41] << alpha[42] << alpha[43] << alpha[44] << alpha[45] << alpha[46] << alpha[47] << alpha[48] << alpha[49] << endl;
	}

	system("pause");																				//keeps program open
}
Not particularly.

1st loop: See coder777.

2nd loop: Not good. You're setting 25-49, however you are not setting the right value. For loops do not have to start at 0. You should start at 25 & use < 50 as your end value. Also, you will never enter the body of the loop because i = 0 is not >= 25.

3rd loop: This is quite a mess. You're going to be printing all 50 elements 50 times. If you really want the loop, you need to change the body. If you don't want the loop, the body is perfect. For the third loop, try printing element i each time. Then, check if i is the tenth element, outputting endl if it is.
Last edited on
like this?
No.

Line 16: Wrong operator (xor) it should be: i*i
line 19: Infinite loop, Simply: for (i = 25; i < 50; i++)

The last loop:
1
2
3
4
5
6
	for (i = 0; i < 50; i++)
	{
	cout << alpha[1];
if(i % 10) // Note: % -> modulus operator
  cout << endl;
	}


For modulus see:

http://www.cprogramming.com/tutorial/modulus.html
Line 16: The '^' operator is for XOR, not exponentiation.
Line 19: This loop will exit immediately because the i >= 25 test fails right away.

In the second loop, use the loop counter as the index. You don't need that 'x' variable. You don't have to start 'for' loops with the loop counter as zero. There's nothing wrong with doing this:
1
2
3
4
5
6
7
8
9
for (int i = 0; i < 25; i++)
{
    alpha[i] = i * i;   //index squared
}

for (int i = 25; i < 50; i++)
{
    alpha[i] = i * 3;   //index times 3
}


EDIT: Double ninja'd. Friggin' lag!
Last edited on
Infinite loop.

The condition (0 >= 25) is never true, so it's not an infinite loop.
Oops did not look to carefully lol had some mistakes anyways

The for statement is as follows

start;end;increment

Again in your last loop you are still outputting all values 50 times

for (i = 0; i < 50; i++)
do what is between { } 50 times, the key here is to combine the alpha variable with the loop variable.

Now if you really want to impress your teacher you could approach this using only one loop and check if the loop counter is greater than 24 and perform another action then print this all
using 1 for statement.

Understanding of coding foundations is mandatory in debugging most simple issues like these,

Good luck.
Last edited on
Topic archived. No new replies allowed.