0% found this document useful (0 votes)
79 views21 pages

Programming Fundamentals Assignment # 5

The document contains 12 programming questions and their solutions in C++. Each question asks the student to write code to perform a specific task like reading and processing array data, sorting arrays, finding minimum/maximum values, transposing matrices, etc. The student is asked to submit the code and output for each question.

Uploaded by

Malik Ali
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)
79 views21 pages

Programming Fundamentals Assignment # 5

The document contains 12 programming questions and their solutions in C++. Each question asks the student to write code to perform a specific task like reading and processing array data, sorting arrays, finding minimum/maximum values, transposing matrices, etc. The student is asked to submit the code and output for each question.

Uploaded by

Malik Ali
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/ 21

PROGRAMMING FUNDAMENTALS

ASSIGNMENT # 5

NAME: Malik Faiz ur Rehman

ROLL #: FA20-BCS-069

Submitted to: Mrs. Ramshah Ahmad

Submitted date: 21-04-2021


Lab Manual 8
Q1. Read the entries of an array of 10 integers from a user. Compute x
as the average of the 10 entries and then compute the average and
display those entries that are greater than or equal to x. Print this final
average.
Code:
#include<iostream>
using namespace std;
int main()
{
int arr[10],i;
float avg=0;
cout<<"Enter 10 values:";
for(i=0;i<10;i++)
{
cin>>arr[i];
avg=avg+arr[i];
}
avg=avg/10;
cout<<endl<<"Average="<<avg;
cout<<endl<<"Entries greater than or equal to "<<avg<<":";
for(i=0;i<10;i++)
if(arr[i]>(avg-1))
cout<<endl<<arr[i];
return 0;
}
Output:

Q2. Write a C++ code to find the minimum and maximum distance
between two numbers of an array.
Code:
#include<iostream>
using namespace std;
int main()
{
int arr[10],i,max=0,min,j,k;
cout<<"Enter 10 numbers for an array:";
for(i=0;i<10;i++)
cin>>arr[i];
min=arr[0];
for(i=0;i<10;i++)
{
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
for(i=0;i<10;i++)
{
if(arr[i]==max)
j=i;
if(arr[i]==min)
k=i;
}
if(j<k)
{
cout<<"Distance between "<<max<<" and "<<min<<"="<<k-j-1;
}
else
{
cout<<"Distance between "<<max<<" and "<<min<<"="<<j-k-1;
}
return 0;
}
Output:

Q3. Take input 10 numbers from user, sort them in ascending and
descending order.
Code:
#include<iostream>
using namespace std;
int main()
{
int arr[10],i,j,k,l;
cout<<"Enter 10 numbers for an array:";
for(i=0;i<10;i++)
cin>>arr[i];
cout<<"ASCENDING ORDER:\n";
for(i=0;i<10;i++)
{
for(l=0;l<10;l++)
for(j=0;j<9;j++)
if(arr[j]>arr[j+1])
{
k=arr[j];
arr[j]=arr[j+1];
arr[j+1]=k;
}
cout<<arr[i]<<" ";
}
cout<<"\nDESCENDING ORDER:\n";
for(i=0;i<10;i++)
{
for(l=0;l<10;l++)
for(j=0;j<9;j++)
if(arr[j]<arr[j+1])
{
k=arr[j];
arr[j]=arr[j+1];
arr[j+1]=k;
}
cout<<arr[i]<<" ";
}
return 0;
}
Output:

Q4. Take array of 5 numbers from user, now print them in reverse
order.
Code:
#include<iostream>
using namespace std;
int main()
{
int arr[5],i;
cout<<"Enter 5 numbers for an array:";
for(i=4;i>=0;i--)
cin>>arr[i];
cout<<endl<<"REVERSED ARRAY\n";
for(i=0;i<5;i++)
cout<<arr[i]<<" ";
return 0;
}
Output:

Q5. Take 10 float numbers from user, now find second greatest
number from array.
Code:
#include<iostream>
using namespace std;
int main()
{
float arr[10],max=0;
int i;
cout<<"Enter 10 float numbers for an array:";
for(i=0;i<10;i++)
cin>>arr[i];
for(i=0;i<10;i++)
if(arr[i]>max)
max=arr[i];
for(i=0;i<10;i++)
if(arr[i]==max)
arr[i]=0.0;
max=0;
for(i=0;i<10;i++)
if(arr[i]>max)
max=arr[i];
cout<<"Second largest float number="<<max;
return 0;
}
Output:

Q6. Take array of 10 numbers, now find smallest number in array


and make it the greatest number in array and then print new array.
Code:
#include<iostream>
using namespace std;
int main()
{
int arr[10],min,i,max=0;
cout<<"Enter 10 numbers for an array:";
for(i=0;i<10;i++)
cin>>arr[i];
min=arr[0];
for(i=0;i<10;i++)
if(arr[i]<min)
min=arr[i];
for(i=0;i<10;i++)
if(arr[i]>max)
max=arr[i];
for(i=0;i<10;i++)
if(arr[i]==min)
arr[i]=max+1;
for(i=0;i<10;i++)
cout<<arr[i]<<" ";
return 0;
}
Output:

Q7. Take 10 numbers from user, now display most occurring


element and also its number of occurrence.
Code:
#include<iostream>
using namespace std;
int main()
{
int arr[10],occ=1,m_occ=0,i,j;
cout<<"Enter 10 numbers for an array:";
for(i=0;i<10;i++)
cin>>arr[i];
for(i=0;i<10;i++)
{
occ=1;
for(j=i+1;j<10;j++)
{
if(arr[i]==arr[j])
occ=occ+1;
}
if(occ>m_occ)
m_occ=occ;
}
for(i=0;i<10;i++)
{
occ=1;
for(j=i+1;j<10;j++)
{
if(arr[i]==arr[j])
occ=occ+1;
}
if(occ==m_occ)
cout<<arr[i]<<" has occurred "<<m_occ<<" times.\n";
}
return 0;
}
Output 1:

Output 2:

Q8. Write a C++ program to generate the sum of left diagonal.


Code:
#include<iostream>
using namespace std;
int main()
{
int arr[3][3],i,j,sum=0;
cout<<"Enter numbers for a 3x3 matrix:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>arr[i][j];
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(i==j)
sum=sum+arr[i][j];
}
cout<<"Sum of left diagonal="<<sum;
return 0;
}
Output:
Q9. Write a C++ program to find the duplicate values in a 2d array.
Code:
#include<iostream>
using namespace std;
int main()
{
int row, col;
cout<<"Enter rows for 2D Array:";
cin>>row;
cout<<"Enter columns for 2D Array:";
cin>>col;
int array[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<"Enter value for Array["<<i<<"]["<<j<<"] : ";
cin>>array[i][j];
}
}
int count;
int duplicate[20];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
count = 0;
for(int k=0;k<row;k++)
{
for(int l=0;l<col;l++)
{
if(array[i][j] == array[k][l])
{
count++;
}
}
}
if(count>1)
{
cout<<"Duplicate Value Found : "<<array[i][j]<<endl;
}
}
}
return 0;
}
Output:

Q10. Write a C++ program to move all negative elements of an


array of integers to the end of the array without changing the order
of positive element and negative element.
Code:
#include<iostream>
using namespace std;
int main()
{
int arr[10],arr1[10],i,j,k,l;
cout<<"Enter 10 numbers for an array:";
for(i=0;i<10;i++)
cin>>arr[i];
i=0;
for(j=0;j<10;j++)
{
if(arr[j]>=0)
{
arr1[i]=arr[j];
i++;
}
}
for(j=0;j<10;j++)
{
if(arr[j]<0)
{
arr1[i]=arr[j];
i++;
}
}
for(i=0;i<10;i++)
cout<<arr1[i]<<" ";
return 0;
}
Output:
Q11. Write a C++ Program to store temperature of two different
cities for a week and display it. Find the city with hottest
temperature.
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
float c1[7],c2[7],max1=0,max2=0;
int i;
string city1,city2;
cout<<"Enter the name of the first city: ";
cin>>city1;
cout<<"Enter the name of the second city: ";
cin>>city2;
cout<<"Enter the temperatures from Monday to Sunday for "<<city1<<":";
for(i=0;i<7;i++)
cin>>c1[i];
cout<<"Enter the temperatures from Monday to Sunday for "<<city2<<":";
for(i=0;i<7;i++)
cin>>c2[i];
for(i=0;i<7;i++)
{
if(c1[i]>max1)
max1=c1[i];
}
for(i=0;i<7;i++)
{
if(c2[i]>max2)
max2=c2[i];
}
if(max1>max2)
cout<<city1<<" has the highest temperature of "<<max1<<"C.";
else if(max2>max1)
cout<<city2<<" has the highest temperature of "<<max2<<"C.";
else
cout<<"The highest temperature of "<<city1<<" and "<<city2<<" is equal.";
return 0;
}
Output 1:

Output 2:
Q12. Write a C++ program to generate transpose of 3˟3 matrix.
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[3][3],i,j;
cout<<"Enter entries for a 3x3 matrix:";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>arr[i][j];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<arr[j][i];
cout<<endl;
}
return 0;
}
Output:

You might also like