Arrays in C
Arrays in C
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should store:
string cars[4];
int marks [5];
Array Initialization:
Int marks [5] = {2, 3 4, 5, 8}
String cars[3]= {“BMW”, “Mehran”, “Volvo”}
Array Types:
One Dimensional
Two Dimensional
Multi-Dimensional
Practice Questions
Q1: Write a program in C++ using arrays that will take an array of size 15 and
store the students names. Then print all the students’ names.
#include <iostream>
int main()
cout<<"Students List"<<endl;
{
cout<<students[i]<<endl;
return 0;
Q2: Write a program in C++ using arrays that will take names of the students from
the user.
// Online C++ compiler to run C++ program online
#include <iostream>
int main()
string students[5];
cin>>students[i];
return 0;
Q3: Write a program that store three values in an array and find the largest value.
#include <iostream>
int large[3];
cin>>large [i];
if (large[0]>large[1]&&large[0]>large[2])
cout<<large[0]<<"is largest"<<endl;
else if (large[1]>large[0]&&large[1]>large[2])
cout<<large[1]<<"is greater"<<endl;
else
cout<<large[2]<<"is largest"<<endl;
return 0;
Q4: Write a program in C++ using arrays that will print the table of user choice
and display the result.
int main()
cin>>num;
for(int i=0;i<10;i++)
cout<<num<<"x"<<arr[i]<<"="<<num*arr[i]<<endl;
return 0;
Q5: Write a C++ program using arrays that will take the marks 0f 17 students and
print the number of students passed. The passing marks are greater and equal to
60.
#include <iostream>
int main()
cin>>marks[i];
if(marks[i]>=60)
pass=pass+1;
}}
return 0;