Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pflab 10

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Programming Fundamentals

Lab-10
Task-1:
Write a program that declares an array of 5 float numbers. Use a loop to read 5
real numbers from user and fill the array. Then print the following on screen: a.
All the elements of array b. The average of all the numbers in the array c. The
numbers below average d. The numbers above average.

Code:
#include<iostream>
using namespace std;
int main()
{
float nums[5],avrg[5];
float average=0;

cout<<"Enter 5 numbers:"<<endl;
for (int i=0;i<5;i++)
{
cin >>nums[i];
}

cout<<"-> Part 'a':"<<endl;


for (int i=0;i<5;i++)
{
cout<<nums[i]<<" ,";
average+=nums[i];
}
cout<<endl;

cout<<"-> Part 'b':"<<endl;

average/=5;
cout<<"Average value of give numbers = "<<average<<endl;

cout<<"-> Part 'c':"<<endl;


cout<<"bellow average numbers"<<endl;
for(int i=0; i<5;i++)
{
if (nums[i]<average)
{
cout<<nums[i]<<" ,";
}
}

cout<<endl;
cout<<"-> Part 'd':"<<endl;
cout<<"above average numbers"<<endl;
for(int i=0; i<5;i++)
{
if (nums[i]>average)
{
cout<<nums[i]<<" ,";
}
}

return 0;
}

Output window:
Task 2:

Code:
#include <iostream>
using namespace std;
int main()
{
int grade[]={64,36,56,47,40,54,61,60,58,64,54,48,59,45,63,54,50,49,51,60,58,59};
int siz=sizeof(grade),ming=grade[0],maxg=grade[0];
int num,time=0;
float avrg=0;

siz=siz/sizeof(grade[0]);
cout<<"number of grades = "<<siz<<endl;
int index[siz];
cout<<"grades are:";
for (int i=0;i<siz;i++)
{
cout<<grade[i]<<" ,";
avrg+=grade[i];
if (ming>grade[i])
{
ming=grade[i];
}
else if (maxg<grade[i])
{
maxg=grade[i];
}
}
cout<<endl;
avrg/=siz;
cout<<"Minimum grade in list ="<<ming<<endl;
cout<<"maximum grade in list ="<<maxg<<endl;
cout<<"Average grade ="<<avrg<<endl;

cout<<endl<<"Enter the marks for further details"<<endl;


cin >>num;
for(int i=0;i<siz;i++)
{
if (num==grade[i])
{
index[time]=i;
time++;
}
}
cout<<"the entered numbers are "<<time<<" times"<<endl;
cout<<"the given number are available at index:"<<endl;
if (time==0)
{
cout<<"there is no such number"<<endl;
}
else
{
for(int i=0; i<time;i++)
{
cout<<index[i]<<" ,";
}
}

return 0;
}

Output Window:

Task 3:
Create a C++ program to determine if an array of integers is a palindrome. An array is a
palindrome if it reads the same forwards and backwards. For example, the array [1, 2, 3, 2,
1] is a palindrome, while the array [1, 2, 3, 4, 5] is not.

Code:
#include<iostream>
using namespace std;
int main()
{
int i, j=5,check=0;
int pal[j];
cout<<"Enter your set."<<endl;
for (i=0; i<j;i++)
{
cout<<i+1<<". ";
cin >>pal[i];
}
i=0;
j=4;
while (i<j)
{
if(pal [i]!=pal [j])
{
check=1;
}
i++;
j--;
}
if(check==0)
{
cout<<"Yes it is palndrome"<<endl;
}
else
{
cout<<"It is not palendorme";
}
return 0;
}

Output window:
Task 4:
Write a C++ program that initializes an integer array of 10 elements, defines a function
named "access3" which accepts the entire array as its parameter, and within "access3"
function, adds 3 to each element of the array.

Code:
#include<iostream>
using namespace std;

void access3(int [],int);


int main()
{
int num[10];
cout<<"Before access3"<<endl;
for (int i=0;i<10;i++)
{
num[i]=i+1;
cout<<num[i]<<",";

}
cout<<endl;
access3(num,10);
cout<<"After access3"<<endl;

for(int i=0;i<10;i++)
{
cout<<num[i]<<",";
}

return 0;
}

void access3(int get[],int x)


{
for(int i=0;i<x;i++)
{
get[i]=get[i]+3;
}
}

Output window:
Task 5:
Create a C++ program that prompts the user to input 10 numbers into an array. Then,
determine the total count of positive, negative, even, and odd numbers in the array.

Code:
#include<iostream>
using namespace std;
int main()
{
int user[10];
int p=0,n=0,e=0,o=0;
for(int i=0;i<10;i++)
{
cout<<"Enter "<<i+1<<" Number : ";
cin >>user[i];
if (user[i]>0)
{
p++;
}
else if(user[i]<0)
{
n++;
}
if(user[i]%2==0)
{
e++;
}
else if(user[i]%2!=0)
{
o++;
}
}
cout<<"number of positive numbers are : "<<p<<endl;
cout<<"number of negative numbers are : "<<n<<endl;
cout<<"number of Odd numbers are : "<<o<<endl;
cout<<"number of even numbers are : "<<e<<endl;
return 0;
}
Output window:

You might also like