In: Computer Science
C++
I have to create printArray, but I have the error " error C2065: 'ar': undeclared identifier". Just need a little help.
#include
#include
#include
using namespace std;
void fillArray(int a[], int size);
void printArray(int a[], int size);
int main()
{
int ar[10];
fillArray(ar, 10);
printArray(ar, 10);
return 0;
}
void fillArray(int a[], int size)
{
for(int i = 0; i < 10; i++)
a[i] = rand() % 10 + 1;
}
void printArray(int a[], int size)
{
for (int i = 0; i < 10; i++)
cout << ar[i];
}
Coming to your code in that function you passing an array named a[] not ar[] array so you got ar[] is not defined in the scope
so you make change of ar[] to a[] or pass ar[] in function definition in order to run code successfully
here i made ar[] as a[] now the code is running without an error
Here is the modified code
#include <stdio.h>
#include <cstdlib>
#include <iostream>
using namespace std;
void fillArray(int a[], int size);
void printArray(int a[], int size);
int main()
{
int ar[10];
fillArray(ar, 10);
printArray(ar, 10);
return 0;
}
void fillArray(int a[], int size)
{
for(int i = 0; i < 10; i++)
a[i] = rand() % 10 + 1;
}
void printArray(int a[], int size)
{
for (int i = 0; i < 10; i++)
cout << a[i] << "\n" ;
}
Output
Any queries regarding this comment please