Help with function arrays

Apr 15, 2016 at 2:02pm
Hi, I'm learning about functions and arrays. I'm trying to use for loop to assign values to the array but it isnt working...
this is the code:

#include <iostream>
using namespace std;
//void Arr(int a[5]);

void Arr(int a[]){
// int b;
cout<<"Values of a= ";
// for(int i=0; i<5; i++){
// a[i];
// b=a[i];
// cout<<b;
}

// }
int main(){

Arr(a);
int b;
cout<<"Values of a= ";
for(int i=0; i<5; i++){
a[i];
b=a[i];
cout<<b;
}
// for(int i=0; i<5; i++){
// a[i];}
return 0;
}
Apr 15, 2016 at 2:45pm
closed account (48bpfSEw)
This // is a comment! They are ignored even if C++ commands are involved! ^^



1
2
3
4
5
void Arr (int a[]) {
  for(int i=0; i<5; i++) {
    cout << "Values of a[" << i << "] = " << a[i] << endl;
   }
}


should work!
Apr 16, 2016 at 6:52am
soryy about that I actually made them comments myself incase I needed to go back to them or something.. I tried the code you stated above but for some reason it is still giving the value 304500... I dont understand whats wrong. this is how I did it using your code:
#include <iostream>
using namespace std;

void Arr (int a[]) {
for(int i=0; i<5; i++) {
cout << "Values of a[" << i << "] = " << a[i] << endl;
}
}
int main(){
int a[5];
Arr(a);


return 0;
}
Apr 16, 2016 at 3:49pm
closed account (48bpfSEw)
You did not initialized your area a[]
therefore it has any random numbers!

do this in your main routine:

for (int i=0;i<5;i++) a[i] = 0;

then call Arr(a);
Last edited on Apr 16, 2016 at 3:49pm
Topic archived. No new replies allowed.