Arrays( I have the code but can you confirm it 100000%)

Hello guys, the question is:
Generate code to declare an integer array,Int_Var, of size 100 and write a C++ Program to carry out the following steps:

a) Assign each array element an initial value of 0;
b) Using a loop to give the first five array elements the following values: Int_Var[0]=0;
Int_Var[1]=1; Int_Var[2]=2; Int_Var[3]=3; Int_Var[4]=4.

c) Using a loop to determine the values for each integer to be beginning from the array element Int_Var[4]:
Array element Int_Var[4];
Int_Var[i]=Int_Var[i-1] + Int_Var[i-2];


And this the code, Can you please confirm it:

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

#include<iostream>

int int_Var[100]; //global variable

void main(void) {

for(int i = 0; i <= 100;i++){ // Array initialization

int_Var[i] = 0;

}

for (int ii = 0; ii<= 4;ii++){ //assigning the value 0-4 in the array
int_Var[ii] = ii;
}
for(int y = 4; y<= 100; y++){ // Output the value of the array
int value ;
value = int_Var[y];

cout<<"The Value of the Array beginning from array index 4 is :"<<" "<<value<<"\n";
}
}


Can you please correct it for me as it is not being compiled. There is an error that I don't know where. And can you please tell me if this code answers the question perfectly.

The error I get is:
error C2065: 'cout' : undeclared identifier


Thanks in advance
Last edited on
You aren't using namespace std so you will need to add std::cout as a prefix.

Also what's with the random global variable? Seems useless and works if you put it in main(). Main should also be int main(), as void main() isn't standard.
so this is the new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int int_Var[100]; //global variable

int main() {

for(int i = 0; i <= 100;i++){ // Array initialization

int_Var[i] = 0;

}

for (int ii = 0; ii<= 4;ii++){ //assigning the value 0-4 in the array
int_Var[ii] = ii;
}
for(int y = 4; y<= 100; y++){ // Output the value of the array
int value ;
value = int_Var[y];

cout<<"The Value of the Array beginning from array index 4 is :"<<" "<<value<<"\n";
return 0;
}
}


The output I am getting is the following:

The Value of the Array beginning from array index 4 is : 4



Is this output correct according to the question asked?

can you check this for me pls.

Thanks in responce.
Can anyone answer me
You didn't do this:
c) Using a loop to determine the values for each integer to be beginning from the array element Int_Var[4]:
Array element Int_Var[4];
Int_Var[i]=Int_Var[i-1] + Int_Var[i-2];


What should this do?
1
2
3
4
5
6
7
for(int y = 4; y<= 100; y++){ // Output the value of the array
int value ;
value = int_Var[y];

cout<<"The Value of the Array beginning from array index 4 is :"<<" "<<value<<"\n";
return 0;
}
Move the return 0 outside of the third for loop.
And you forgot another loop for setting the variables according to Int[i-1]+Int[i-2]
So this is the code till now,

can somebody help me in part c, I don't really get it.

Thanks



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int int_Var[100]; //global variable

int main() {

for(int i = 0; i <= 100;i++){ // Array initialization

int_Var[i] = 0;

}

for (int ii = 0; ii<= 4;ii++){ //assigning the value 0-4 in the array
int_Var[ii] = ii;
}
for(int y = 4; y<= 100; y++){ // Output the value of the array
int value ;
value = int_Var[y];

cout<<"The Value of the Array beginning from array index 4 is :"<<" "<<value<<"\n";
}
return 0;
}






can somebody help me in part c, I don't really get it.
Thanks
Part C: make a loop starting from 4 to the array end and inside it you should have this line: Int_Var[i]=Int_Var[i-1] + Int_Var[i-2];
I added the following code lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int int_Var[100]; //global variable

int main() {

for(int i = 0; i <= 100;i++){ // Array initialization

int_Var[i] = 0;

}

for (int ii = 0; ii<= 4;ii++){ //assigning the value 0-4 in the array
int_Var[ii] = ii;
}
for(int y = 4; y<= 100; y++){ // Output the value of the array
int value ;
value = int_Var[y];
int_Var[i]=int_Var[i-1] + int_Var[i-2];
cout<<"The Value of the Array beginning from array index 4 is :"<<" "<<value<<"\n";
}
return 0;
}


I am getting syntax error, could someone modify the code and give me it in order for it to work perfectly please.
Ok guys, I got the following, Is this true:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
int int_Var[100];

int main() {

for(int i = 0; i <= 100;i++){ 

int_Var[i] = 0;

}
for (int ii = 0; ii<= 4;ii++){ 
int_Var[ii] = ii;
}
for(int y = 4; y<= 100; y++){ 

int_Var[y]=int_Var[y-1]+int_Var[y-2];
int value ;
value = int_Var[y];

cout<<"The Value the Array beginning from array index 4 is :"<<" "<<value<<"\n";
}
return 0;
}
You don't need lines 19 and 20, you can just show int_Var[y]
So it is finally like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int int_Var[100];
int main() {
for(int i = 0; i < 100;i++){ 
int_Var[i] = 0;
}
for (int ii = 0; ii< 4;ii++){ 
int_Var[ii] = ii;
}
for(int y = 4; y< 100; y++){ 
cout<<"The Value the Array beginning from array index 4 is :"<<" "<<value<<"\n";
}
return 0;
}


I am getting in the output some negative values. Is is correct. Can array values be negative?

You removed line 17 of your previous code, that was an important one
Oh, yes, now it is working, can I ask you, if you debug the code below, you will get some negative values. For example, I get the following output:

The Value the Array 46 beginning from array index 4 is - then a number.


How come I get a negative number, altough, if you use the equation, we can't get a -ve number???



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int int_Var[100];
int main() {
for(int i = 0; i < 100;i++){ 
int_Var[i] = 0;
}
for (int ii = 0; ii< 4;ii++){ 
int_Var[ii] = ii;
}
int z;
int value ;
for(int y = 4; y< 100; y++){ 
int_Var[y]=int_Var[y-1]+int_Var[y-2];
value = int_Var[y];
z=y;
cout<<"The Value the Array "<<z<< "\t"; cout<< "beginning from array index 4 is :"<<" "<<value<<"\n";
}
return 0;
}
Last edited on
The negative values are caused by too high values, try to change int_Var to an unsigned long array
Exactly. I did it as the following and I got the great answers. BUT, I still think that there is something wrong, I thing I the output numbers are really very very big when comparing them to the equation. Do you have an explanation to this.
For example, for y = 10, using the equation, I should get
int_Var[10]= int_Var[10-1]+int_Var[10-2] = 9+8 = 17.

Is this correct or am I misunderstanding the calculations.



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

#include<iostream>
using namespace std;
unsigned long  int_Var[100];
int main() {
for(int i = 0; i < 100;i++){ 
int_Var[i] = 0;
}
for (int ii = 0; ii< 4;ii++){ 
int_Var[ii] = ii;
}
int z;
unsigned long  value ;
for(int y = 4; y< 100; y++){ 
int_Var[y]=int_Var[y-1]+int_Var[y-2];
value = int_Var[y];
z=y;
cout<<"The Value the Array "<<z<< "\t"; cout<< "beginning from array index 4 is :"<<" "<<int_Var[y]<<"\n";
}
return 0;
}



Thank you a lot. You were of massive help.

Thanks again.
int_Var[10-1] is the value of int_Var[9], not 9
so:
for int_Var[4], the value is 3+2 = 5
for int_Var[5], the value is 5+3 = 8
etc
NOW I got it 100%

Thanks Bazzy, you were really of great help ans sorry for the many questions
Thanks.

TipTip
Topic archived. No new replies allowed.