You are on page 1of 11

Computer programming lab

2020

LAB REPORT

Submitted by: Muhammad subhan ghani

Submitted to: Engr. Abdullah

Registration no: 1826

Class Section: C
Objectives:

To understand the programming knowledge using Array To


understand the programming using array Tasks:

1. Write a program to input twelve numbers from user using array and display all values on
console (3 values in a row) (use separate loops for input and output operation).
SAMPLE OUTPUT:
Value 1=?? Value 2=?? Value 3= ??
Value 4=?? Value 5=?? Value 6= ??
Value 7=?? Value 8=?? Value 9= ??
Value 10=?? Value 11=?? Value 12= ??
#include<iostream>

using namespace std; int

main()

{
int x[12]; int

h=0; for(int

i=0;i<12;++i)

cout<<"enter value# "<<i+1<<"";

cin>>x[i];

for(int i=1;i<5;i++)

for(int j=0;j<3;j++)

cout<<"value #"<<h+1<<"="<<x[h]<<"\t";

h++;

cout<<endl;

}
2. Write a program to input ten numbers from user using array and find the average of numbers
(use separate loops for input operation and calculation).

#include<iostream>

using namespace std; int

main()

int x[10]; float

avg,sum=0; cout<<"plz enter 10

num"<<endl; for(int

i=0;i<10;i++)

cin>>x[i];

sum=sum+x[i];

}
avg=sum/10; cout<<"average of the num u

entered is"<<avg;

3. Write a program to input multiple values from user using array and find the maximum ( use
separate loops for input operation and calculation ).

#include<iostream>

using namespace std; int

main()

int i,k; float arr[100]; cout<<"enter

total numof element1 to 100:"; cin>>k;

cout<<endl;

for(i=0;i<k;++i)
{

cout<<"enter num"<<i+1<<":";

cin>>arr[i];

for(i=1;i<k;i++)

if(arr[0]<arr[i])

arr[0]=arr[i];

cout<<"largest element="<<arr[0];

4. Write a program to input two arrays from user and find the sum of arrays (element by

element). #include<iostream> using namespace std; int main()

{
int x[3] ,y[2],sum=0;

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

{ cout<<"enter value for #"<<i+1<<"for

variablex\n";

cin>>x[i];

cout<<"enter value for #"<<i+1<<"for variable y\n";

cin>>y[i];

cout<<endl;

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

{ sum=x[i]+y[i];

cout<<"the sum of"<<i+1<<"is"<<sum<<endl;

}
5. Write a program to input array from user and display the elements of array in reverse order
(use separate loops for input and output operation).

SAMPLE OUTPUT:
Input = 1, 2, 3, 4, 5
Output = 5, 4, 3, 2, 1
#include<iostream>

using namespace std; int

main()

int n,c,d,a[100],b[100]; cout<<"enter

the num of element and array"; cin>>n;

cout<<"enter array element"<<endl;

for(c=0;c<n;c++) cin>>a[c];
for (c=n-1,d=0;c>=0;c--,d++)

b[d]=a[c]; for(c=0;c<n;c++)

a[c]=b[c];

cout<<"\n\nreverse array is \n";

for(c=0;c<n;c++)

cout<<a[c]<<endl;

6. Write a program to input multiple values from user using array and then ask user to input
a key number and then compare it with values entered by user(for array) and then
display the index number of array with which value get matched (use seperate loops for
input operation and calculation) (use different loops for input operation and calculation).
#include<iostream>
using namespace std; int

main()

int x[100],n,m,j,key; bool

cond=false; cout<<"enter

element of array"; cin>>n;

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

cout<<"enter value#"<<i+1<<":";

cin>>x[i];

cout<<"\n enter key

number:"; cin>>key;

for(j=0;j<n;j++)

if(x[j]==key)

cond==true;

m=j; }

if(cond)
cout<<"key you entered is matched"<<m<<"index of

array"; else cout<<"does not matched";

You might also like