Not sure what this question is asking me to program?

closed account (oN3h0pDG)
Just learnt about arrays today so I'm not very good at them. But I have a practice question where it asks me:

First, declare an array of 50 integers.
Set the first value in the array (i.e. the value at offset 0) to 0.
Next, set each array element at offset x to be equal to the value of log(x). Note: in this step, we start with x = 1 (i.e., we leave the zero from step 2). Don't do this the long way - use a loop!
Finally, go through the array again. For each array element, look at the value stored and display that number of spaces (" "). At the end of the row of spaces, display a single '*' character, followed by an end-line.

It may be that it's very late right now while I'm trying to do this or I'm just being stupid about it but I'm unsure what they're trying to get me to program in step 3 and 4. I did 1 and 2 but i'm not sure what to do for 3 and 4. Thanks for your help, it is appreciated.

This is what I did so far and I'm unsure if I'm on the right path so any help would be great, thanks! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
#include <math.h>

using namespace std;

int main() {

	int integerArray[50];

	integerArray[0] = 0;

	for (int x = 1; x <= 50; x++) {

	integerArray[50] = log(x);

	cout << x << endl;

	}
	return 0;
}
Okay, what you are doing on line 15 is setting the 50th element (which doesn't exist, there are 0 to 49) 50 times to the value of the logarithm of x. What you need to do to fix this problem is change the 50 to x, like so: integerArray[x] = log(x);. And because the maximum element number is 49 you would need to change the condition in the for loop to a < rather than a <= operator.

Secondly, because you are asked to display an amount of spaces equal to the value of x you would do a second for loop around line 17. You would write something like: for (int y = 0; y < integerArray[x]; y++). Then inside that loop would be the line to output a single space. This loop would go through every element and keep outputting spaces until the logarithm value of that number is reached if you get me.

But other than that you are definitely on the right lines.
Last edited on
Topic archived. No new replies allowed.