I am learning C and had an assignment that asked to take two numbers and insert them into an array. First number is the base number the second number was the array length. The length of the array would be the number of times the base was multiplied by the exponent.
For example, if base was 2 and array length was 4 then in element 0 would be 1, element 2 would be 2, etc. and the output would be 2, 4, 8, 16.
This worked, up to putting in any base number and then 5. If the length was 5 or above I would get the first four elements correctly then everything after would be a 0.
This is the code I came up with and did not work as expected.
#include <iostream>
using namespace std;
int main() {
int power, powarray[power];
double base;
while(power >= 0) {
cout << "Please enter a base number.\n";
cin >> base;
cout << "Please enter your exponent.\n";
cin >> power;
if (power <= 0) {
cout << "Please enter a nonnzero power\n";
}
else if (power > 0) {
for(int n = 0; n < power; n ) {
//powarray[0] = base;
powarray[n] = base * powarray[n-1];
cout << powarray[n] << endl;
}
}
}
return 0;
}
Any help is appreciated.
Sienna
CodePudding user response:
I would first fix your code indentation to make code blocks more clear:
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int power, powarray[power];
7 double base;
8
9 while(power >= 0) {
10
11 cout << "Please enter a base number.\n";
12 cin >> base;
13 cout << "Please enter your exponent.\n";
14 cin >> power;
15
16 if (power <= 0) {
17 cout << "Please enter a nonnzero power\n";
18
19 } else if (power > 0) {
20
21 for(int n = 0; n < power; n ) {
22
23 //powarray[0] = base;
24 powarray[n] = base * powarray[n-1];
25 cout << powarray[n] << endl;
26 }
27 }
28 }
29
30 return 0;
31 }
Now, here are the main issues:
You are trying to use the value of
power
variable twice (lines6
and9
) before even initializing it. As others mentioned, in C you can't initialize an array with a variable like this. Start simple and use a constant to initialize the array maximum size (for example,100
). Line9
doesn't make much sense, remove thiswhile
loop;At the first iteration of the
for
loop,n
equals0
so line24
will attempt to get a negative index frompowarray
. This is not valid;Code structure is confusing. Let's tidy it up by splitting it into three parts: 1: get input; 2: fill array, calculating members values; 3: print the array. Also, remove commented code.
These lead to something like this:
#include <iostream>
using namespace std;
int main()
{
int power;
double base;
int powarray[100];
/* Get and validate user input */
cout << "Please enter a base number: ";
cin >> base;
cout << "Please enter your exponent: ";
cin >> power;
if (power <= 0 || power > 100) {
cout << "Please enter a positive power not exceeding " << 100 << endl;
return -1;
}
/* Fill array */
for (int n = 0; n < power; n ) {
if (n > 0)
powarray[n] = base * powarray[n - 1];
else
powarray[n] = base;
}
/* Print array */
cout << "Array = ";
for (int n = 0; n < power; n ) {
cout << powarray[n] << " ";
}
cout << endl;
return 0;
}
Here is a sample output:
$ ./a.out
Please enter a base number: 2
Please enter your exponent: 12
Array = 2 4 8 16 32 64 128 256 512 1024 2048 4096
CodePudding user response:
You are mistaken in assuming that the array works for indexes up to 4, you just had bad luck in not observing any visible misbehaviour for lower sizes.
The reasons why at a certain point you do observe misbehaviour:
- You are using a C style array in C which is hardly ever a wise move.
- You are using a non-C VLA (variable length array).
- You are using it incorrectly, even in C it would not work with a non-initialised variable as size.