Help building and printing array

I'm trying to write a program to fill an array with the following conditions:

1:) User defines number of rows up to 25. (array[25])

2:) User defines the first number to start with ( array[0] ).

3:) User defines the increment of the first number to fill the rest of the arrays elements (array[1-n])

I have been trying many variations and tutorials but I haven't been able to crack it.

Here's what I have so far:

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
 #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

int main()
{
	int index[25];
	int incr, elementnum, value = 0;

	cout << "How many elements? (Up to 25) > ";
	cin >> elementnum;
	cout << endl;
	cout << "Number to start with?  > ";
	cin >> value;
	cout << endl;
	cout << "Increment? > ";
	cin >> incr;

	for (int i = 0; i < elementnum; i++){
		int x = value + incr;
		index[i] = x;
		cout << x << "\n";
}
	
	system("pause");
    return 0;
}


In this instance I get if the user puts in 4, 3, 2 it displays
5
5
5
5

It is performing the addition 1 time and putting it into array[0] then repeating but does not increment.

Once this is complete I'm going to have to use each element value to figure square, square root, cube, cube root, in different arrays so any advance advice is welcome also.
1) You are redeclaring x for each iteration of your for loop.

2) You are assigning x the value of value + incr each time, which will remove whatever was in x beforehand (IE: the total up to that point); not x + value + incr
(You can do this easily using the following:
x += value + incr; // This is shorthand for x = x + value + incr; )

Additionally, why not add the endl 's to the beginning of each cout following the previous?
Ex:
1
2
3
4
5
6
7
8
	cout << "How many elements? (Up to 25) > ";
	cin >> elementnum;

	cout << endl << "Number to start with?  > ";
	cin >> value;

	cout << endl << "Increment? > ";
	cin >> incr;


It doesn't change anything, but it is definitely more concise
Last edited on
OK this is the right start.
I have changed the code to reflect your advice.
It is now at least building the array and incrementing but the output still isn't just right.
If I choose to have the following inputs:
Rows 4
Start 3
Increment 2
It prints the following:
5
10
15
20

So the number of elements is correct but it needs to be:
3
5
7
9

I need to have int value assigned to index[0] then add the increment to make the new value and apply to rest of the elements
Thanks for the direction edge6768. I have worked the first array out.

I used the following for loop which will take the first value of index[0] and then increment that value by whatever the user inputs (I replaced line 23-27 with the following code):

1
2
3
4
5
6
7
	x = value;

	for (int i = 0; i < elementnum; i++){
		index[i] = x;
		cout << x << "\n";
		x += incr;
}


Next I'm going to use the values from this first array to build 4 more parallel arrays displaying "square, square root, cube, cube root" based on the corresponding value in index[]
Any more advice?

You could do it all within one for loop if you declare the arrays beforehand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cmath>

// Declare arrays

int x = value;
for (int i = 0; i < elementnum; i++){
		x += incr;

		index[i] = x;
                squareAry[i] = (x * x);
                squareRootAry[i] = sqrt(x);
                cubeAry[i] = (x * x * x);
                cubeRootAry[i] = cbrt(x);

		cout << "Number: " << x
                       << "\nSquare: " << squareAry[i]
                       << "\nSqaure Root: " << squareRootAry[i]
                       << "\nCube: " << cubeAry[i]
                       << "\nCube Root: " << cubeRootAry[i] << '\n';
}


Assuming you don't need to write functions for square, root, cube, and cubic root
Last edited on
Topic archived. No new replies allowed.