0% found this document useful (0 votes)
39 views15 pages

Lab Report 09 Muhammad Abdullah Zafar Gahuri Lab Report 09 ME-14 (C) 405642

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

Lab Report 09 Muhammad Abdullah Zafar Gahuri Lab Report 09 ME-14 (C) 405642

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

Fundamentals of Programming Lab

Lab Report 09

1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section: C Group:
SUBMITTED BY
Sr. Lab
No. Names CMSID Report Lab work Lab Tasks Total
(Theory)
1 Muhammad 405642
Abdullah Zafar
Ghauri

School of Mechanical and Manufacturing Engineering


• Objectives:
1. Array
2. One-Dimensional Array
3. Understand how to locate the address of array
• Theory:
1. Array:
An array in C++ is a collection of elements of the very same data type
placed in different memory locations that can be individually referenced by using
an index to an unique identifier.
Through the use of array thye multiple declaration of variables is replaced with a
single array holding as many variables as possible.
2. One-Dimensional Array:
One-dimensional array is a type of array, being linear .
Accessing the elements of one-dimensional array involves a single array which
can either represent a row or column index.
For example, consider the C declaration int anArrayName[10]; which declares a

one-dimensional array of ten integers. Here, the array can store ten elements of
type int . This array has indices starting from zero through nine. For example, the

expressions anArrayName[0] and anArrayName[9] are the first and last

elements respectively.
3. Address of Array:
By the use of arrays, one can find the address of array in
the memory. For example, int-data type array occupies 4 bits in back-end
memory. The address shall surely be in Hexadecimal order.
Lab Tasks:

Q.1 Write a C++ Program using an array to display the values being initiakized.

Program:

#include<iostream>
using namespace std;
int main()
{
cout<<"\n My name is Abdullah Zafar and i study in ME-14 with CMS code of 405642";
int a[5]; //5 variables of a being int data type of 4 bytereserved.
cout<<"\n PLease enter the first number=";
cin>>a[0];
cout<<"\n Please enter the Second Number=";
cin>>a[1];
cout<<"\n The values are="<<a[0]<<" and " <<a[1]<<"";
return 0;
}
Output:

Q.2 Write a program in C++ using arrays to calculate the sum of arrays.

Input Program:

#include<iostream>

using namespace std;

int main()
{

int list[10];

list[5]=56;

list[2]=54;

list[1]=list[2]+list[5];

cout<<"\n The values are="<<list[1];

return 0;

Output:

Q.3 Write a program in C++ using array with for loop being nested in it.

Input Program:
#include<iostream>
using namespace std;
int main()
{
float six[10];
for(int i=0;i<10;i++)
{
cout<<"Enter value Number="<<i<<":";
cin>>six[i];
}
cout<<"\n The data you entered=";
for(int i=0;i<10;i++)
{
cout<<six[i]<<endl;
}
return 0;
}
Output:

Q.4 Write a program in C++ using arrays while initializing the values to array and printing them
out in a sequence
Input Program:
#include<iostream>
using namespace std;
int main()
{
int name[5]={2,2};
cout<<name[0]<<endl;
cout<<name[1]<<endl;
cout<<name[2]<<endl;
cout<<name[3]<<endl;
cout<<name[4]<<endl;
return 0;
}
Output:
Q.5 Write a program using arrays to determine the memory occupied by array in the
memory(The result shall be in hexa-decimal forn).

Input Program:

#include<iostream>

using namespace std;

int main()

int an[2]={1,2};

cout<<an[0]<<endl;

cout<<an[1]<<endl;

cout<<an;

cout<<&an[0]<<endl; //Here & operator is used to find out address in memory

cout<<&an[1]<<endl;

return 0;

Output:
Lab Assignment:

Q.1 Write a program using arrays that shall take in the input from the user being initialized with
values and uses for loop to display the result

Input Program:

#include<iostream>
using namespace std;
int main()
{
cout<<”\n My name is Abdullah Zafar and I study in ME-14 with CMS ID=405642”;
int k;
int a[5];
a[0]=1;
a[1]=3;
a[2]=5;
a[3]=7;
a[4]=9;
for(k=1;k<=4;k++)
{
cout<<"\n The value in a["<<k<<"] ="<<a[k]<<endl;
}
return 0;
}
Output:

Q.2 Write a program that takes integers in the array and prints them in reverse order.

Input Program:

#include<iostream>
using namespace std;
int main()
{
cout<<”\n My name is Abdullah Zafar and I study in ME-14 with CMS ID=405642”;
int a;
int numerals[5];
for(a=1;a<=4;a++)
{
cout<<"\n Please enter a respective value in element"<<a<<endl;
cin>>numerals[a];
}
cout<<"\n The output in the reverse order="<<endl;
for(a=4;a>=0;a--)
cout<<"\n Values in a ["<<a<<"]"<<numerals[a]<<endl;
return 0;
}
Output:

Q.3 Write a program in array that takes in input of various numbers and calculates the sum and
average of those collection of words.
Input Program:
#include<iostream>
using namespace std;
int main()
{
cout<<"\n My name is Abdullah Zafar and I study in ME-14 with CMS ID=405642";
int sum;
float average;
int numerals[5];
int i;
for(i=1;i<=4;i++)
{
cout<<"\n Please enter the values="<<i<<endl;
cin>>numerals[i];
}
sum=0;
average=0.0;
for(i=4;i>=0;i--)
{
sum=sum+numerals[i];
average=sum/5.0;
cout<<"\n The sum of array values="<<sum<<endl;
cout<<"\n The Average of array values="<<average<<endl;
}
return 0;
}
Output Program:

Q.4 Write a program using array that takes in a variety on input numbers and displays the
maximum value amongst the provided inputs.
Input Program:
#include<iostream>
using namespace std;
int main()
{
cout<<”\n My name is Abdullah Zafar and I study in ME-14 with CMS=405642”;
int x;
int stream[6];
int maxim;
for(x=0;x<=5;x++)
{
cout<<"\n Please enter the value ="<<x<<"=";
cin>>stream[x];

}
maxim=stream[0]; //Initialized the maximum value with value in stream 0 bit
for(x=1;x<=5;x++)
{
if (maxim<stream[x])
maxim=stream[x];
}
cout<<"\n The maximum value="<<maxim<<endl;
return 0;
}
Output:

Q.5 Write a program in C++ that initializes the variables to character type, copying the variables
to a second array of the very same type.
Input Program:
#include<iostream>
using namespace std;
int main()
{
cout<<"\n Ich Heissen sie Abdullah Zafar und Ich studier in ME-14 with CMS
ID=405642"<<endl;
char first[5]={'A','H','M','E','D'};
char second[5];
int a;

for(a=0;a<=4;a++)
{
second[a]=first[a];
}
for(a=0;a<=4;a++)
{
cout<<second[a];
}
return 0;
}
Output Program:

Q.6 Write a program that takes in values in array and displays them on the screen.

Input Program:

#include<iostream>
using namespace std;
int main()
{
cout<<"\n My name is Abdullah Zafar and I study in ME-14 with CMS
ID=405642"<<endl;
double T[6];
int i;
T[0]=56.78;
T[1]=58.34;
T[2]=60.45;
T[3]=65.13;
T[4]=67.54;
T[5]=70.98;

for(i=0;i<=4;i++)
{
cout<<T[i]<<endl;
}
}
Output:
Q.7 Write a program that initializes the array and displays the result on the screen.
Input Program:
#include<iostream>
using namespace std;
int main()
{
char T[5]={'A','B','C','D','E'};
int i;

for(i=0;i<=4;i++)
{
cout<<T[i]<<endl;
}
return 0;
}
Output:

Q.8 Write a program to input values in two different arrays and display the output in third array.
Input Program:
#include<iostream>
using namespace std;
int main()
{
int i;
float a[5];
float b[5];
float c[5];

cout<<"\n Enter the elements for first array";


for(i=0;i<=4;i++)
{
cout<<"\n Enter values in element"<<i<<"=";
cin>>a[i];
}
for(i=0;i<=4;i++)
{
cout<<"\n Enter the values in element"<<i<<"="<<endl;
cin>>b[i];
}
for(i=0;i<=4;i++)
{
cout<<"\n Enter the values in element"<<i<<"="<<endl;
cin>>c[i];
}
return 0;
}

Output:

You might also like