arrays question

aight so im trying to write a program that takes in one value of an array and quaruples it each time for 6 times, i cant get the rest of the numbers to quadruple besides the first, plz help. heres my code:

#include<iostream>

using namespace std;

int main()
{
const int num = 6;
float myArray[6];
float sum = 0;
float quad = 0;
cout << "Please enter a number " << endl;
cin >> myArray[0];

for (int x = 0; x<num; x++)
{
sum = 4*myArray[0];

cout << sum <<" \t";
}
return 0;
}
If I understand you correctly, you want the user to input numbers for each array element. Yeah?
So you have to loop throw the items:
1
2
3
4
for(int i = 0; i < num; i++) {
    cout << "Please enter " << (i+1) << ". number: ";
    cin >> myArray[i];
}

Now you should see what's wrong with your calculation loop ;o)

Firstly, your code only takes in one number. There are no "rest of the numbers". You only put a number into myArray[0], so myArray[1] to myArray[5] will have garbage values in.

That said, when your code gets round to multiplying the numbers, it always multiplies myArray[0].
sum = 4*myArray[0];

Did you mean this?
sum = 4*myArray[x];

Edit: NinJARed again. :(


Last edited on
i will input one value and the program quadruples the last number for example if i input "1", the answer should be 4, 16, 64, 256, ...
Then what exactly is that array for?

1
2
3
4
5
6
7
int x;
cin >> x;
for (int i=0;i<numberOfIterations; ++i)
{
  x = x*4;
  cout << x << " ";
}
Last edited on
heres the question - "Define a float type array with 6 values using a constant value variable. The user should be prompted to fill the first value then the array should be populated by quadrupling the previous value in the array"
You've been given enough example code above now to do that.
@Moschops no i got it i was just responding to the question about what exactly the array was for...i appreciate your help.
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<iostream>

using namespace std;

#define MAX_SIZE 	(6)

int main()
{
 	float myArray[MAX_SIZE];
 	float myNum;
 	
 	cout << "Enter first Number: ";
 	cin >> myNum;
 	
 	//assign the number to the first element of array
 	myArray[0] = myNum;
 	
 	//populate the array
 	for (int i = 0; i < MAX_SIZE; i++)
 	{
	 	myArray[i + 1] = 4 * myArray[i];
	 	
	 	cout << myArray[i] << endl;
	}
	
 	cin.get();
 	return 0;
}



The problem with your code is at
sum = 4*myArray[0];
where you are using only
myArray[0]
and storing it to sum without making any changes to it.

thanks i got it now thanks for all y'all help.
@ThangDo
Did you mean
1
2
3
4
5
6
7
	//populate the array
 	for (int i = 0; i < MAX_SIZE-1; i++)
 	{
	 	myArray[i + 1] = 4 * myArray[i];
	 	
	 	cout << myArray[i] << endl;
	}

?
Or do I miss something? o.O
Last edited on
@shadow123: ah yes, my mistake. otherwise its gonna give an out of bound error.
Topic archived. No new replies allowed.