0% found this document useful (0 votes)
9 views5 pages

Arrays in C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Arrays in C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Arrays:

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>

using namespace std;

int main()

string students[5]={"Amna", "Usman", "Ali", "Ayesha", "Zaid"};

cout<<"Students List"<<endl;

for(int i=0; i<=4;i++)

{
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>

using namespace std;

int main()

string students[5];

cout<<"Enter the students names"<<endl;

for(int i=0; i<=4;i++)

cin>>students[i];

return 0;

Q3: Write a program that store three values in an array and find the largest value.

// Online C++ compiler to run C++ program online

#include <iostream>

using namespace std;


int main()

int large[3];

cout<<"Enter the numbers"<<endl;

for(int i=0; i<=2; i++)

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.

// Online C++ compiler to run C++ program online


#include <iostream>

using namespace std;

int main()

int num, arr[10]={10,9,8,7,6,5,4,3,2,1};

cout<<"Enter the number"<<endl;

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.

// Online C++ compiler to run C++ program online

#include <iostream>

using namespace std;

int main()

int marks[17], pass=0;


cout<<"Enter the students marks"<<endl;

for(int i=0; i<=16; i++)

cin>>marks[i];

if(marks[i]>=60)

pass=pass+1;

}}

cout<<"Number of students passed are "<<pass<<endl;

return 0;

You might also like