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

Roll No 150252 Lab-2: Exercise 1

The document contains 5 programming exercises: 1. Write a program to store a 3x3 matrix, display the matrix and its transpose, and allow the user to continue or terminate the program. 2. Write a program to sort and display a 10-element integer array using bubble sort and insertion sort. 3. Create a student data structure to store details like name, class, marks, and display the information. 4. Create an array of student structures to input details of 10 students, calculate total, average and display names of students with grade B or above. 5. Read a string, convert to character array, sort it based on ASCII and display the sorted array.

Uploaded by

Aleena Kanwal
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)
79 views7 pages

Roll No 150252 Lab-2: Exercise 1

The document contains 5 programming exercises: 1. Write a program to store a 3x3 matrix, display the matrix and its transpose, and allow the user to continue or terminate the program. 2. Write a program to sort and display a 10-element integer array using bubble sort and insertion sort. 3. Create a student data structure to store details like name, class, marks, and display the information. 4. Create an array of student structures to input details of 10 students, calculate total, average and display names of students with grade B or above. 5. Read a string, convert to character array, sort it based on ASCII and display the sorted array.

Uploaded by

Aleena Kanwal
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/ 7

Roll no 150252

Lab-2
Exercise 1
Write a program which can store a matrix data and display transpose of matrix. User should be
able to enter matrix values at runtime and display matrix data and its transpose. Create a
separate function Transpose which accepts matrix as an argument and displays its transpose.
After displaying transpose of matrix, program should give a choice to use whether to continue
and repeat matrix input or terminate.

#include<iostream>
using namespace std;
void Transpose(int a[3][3]);

int main() {
int a[3][3]={0};
cout<<"values";
for(int i=0;i<3;i++)
{for(int j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Matrix A is";
for(int i=0;i<3;i++)
{for(int j=0;j<3;j++)
{cout<<a[i][j];
}cout<<endl;//sapcing
}
cout<<"Transpose=";
Transpose(a);
return 0;
}
void Transpose(int a[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<a[j][i];
}
cout<<endl;
}}
Exercise 2
Write a program to read 10 random integer values into an array.
a) Write a user defined function to sort the array using bubble sort.
b) Write a user defined function to sort array using Insertion sort
NOTE: Array will be passed to these functions by reference from main function
#include <iostream>
using namespace std;
// insershort code//
void insertionshort(int arr[10])
{

int i, j, tmp;

for (i = 1; i < 10; i++) {

j = i;

while (j > 0 && arr[j - 1] > arr[j]) {

tmp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = tmp;

j--;

}
cout<<"Array after Insertion Sort...\n";

for(int k=0;k<10;k++)
{
cout<<" "<<arr[k];
}

}
// bubble short code//
void bubbleshort(int a[10])
{
int b=0;
for(int i=0;i<10;i++)
{for(int j=9;j>i;j--)
{if(a[j]<a[i])
{b=a[j];
a[j]=a[i];
a[i]=b;}
else
continue;
}
}
cout<<"Array"<<endl;
for(int i=0;i<10;i++)
{
cout<<a[i];
}
}

// main code//
int main() {
int a[10]={0};
int c[10]={0};
cout<<"enter vaue";
for(int i=0;i<10;i++)
{
cin>>a[i];
cin>>c[i];
}
cout<<"array before sorting";
for(int i=0;i<10;i++)
{cout<<a[i];
}
bubbleshort(a);
cout<<endl;
insertionshort(c);
return 0;
}

Exercise 3
Create a data structure to store student information including:
- Student name
- Class Name
- Section
- Status (true/false to determine if student is currently at study or left university)
- Marks Obtained
- Total Marks
Initialize structure instance and fill values at runtime from user.
Display student information on screen.
#include<iostream>
using namespace std;
struct Student
{
char name[30],classname[30],section[30];
float sub1,sub2,sub3,sub4;
};
//main code//
int main()
{
Student student;
cout<<"enter the name=";
cin>>student.name;
cout<<"enter the class=";
cin>>student.classname;
cout<<"enter the section=";
cin>>student.section;
cout<<"enter the sub1 number= ";
cin>>student.sub1;
cout<<"enter the sub2 number= ";
cin>>student.sub2;
cout<<"enter the sub3 number: ";
cin>>student.sub3;

student.sub4=student.sub1+student.sub2+student.sub3;
cout<<"name is:"<<student.name<<endl;
cout<<"class is:"<<student.classname<<endl;
cout<<"section: "<<student.section<<endl;
cout<<"sub1 : "<<student.sub1<<endl;
cout<<"sub2 : "<<student.sub2<<endl;
cout<<"sub3 "<<student.sub3<<endl;

cout<<"total marks : "<<student.sub4<<endl;


return 0;
}
Exercise 4
Write a program which creates an array of structure Student (created in question 3)
to read student information at runtime from user (up to 10 students). After filling in
all information:
- Calculate & display grand total of marks obtained by all students
- Calculate & display average of marks obtained by all students
- Calculate grade of students (A is given where marks are greater than 80%)
- Display names of students who have secured grade B or above
Program should repeat itself as many times user wants to.
#include<iostream>
using namespace std;
struct student
{
char name[30],Class[30],sec[30];
int gpa;
float sub1,sub2,sub3,t,a;
};
// main code
int main()
{
int choice='1';
student b[10];
int total,average;
while(choice=='1')
{
for(int i=0;i<10;i++){
cout<<"enter the name=";
cin>>b[i].name;
cout<<"enter the class=";
cin>>b[i].Class;
cout<<"enter the section=";
cin>>b[i].sec;
cout<<"enter the gpa=";
cin>>b[i].gpa;
cout<<"enter sub 1 number= ";
cin>>b[i].sub1;
cout<<"enter thesub2 number= ";
cin>>b[i].sub2;
cout<<"enter the sub3 number ";
cin>>b[i].sub3;
//total//
b[i].t=b[i].sub1+b[i].sub2+b[i].sub3;
b[i].a=b[i].t/3;
if(b[i].a>90)
b[i].gpa=4;
else if(b[i].a>80)
b[i].gpa=3;
else if(b[i].a>60)
b[i].gpa=2;

else
b[i].gpa=0;
total=total+b[i].t;
average=average+b[i].a;
}
average=average/10;
cout<<"Total is = "<<total<<endl;
cout<<" Average of all student="<<average<<endl;
for(int i=0;i<10;i++)
{
if(b[i].gpa==4 || b[i].gpa==3)
{
cout<<"Student name is "<<b[i].name<<endl;
cout<<"class Name is : "<<b[i].Class<<endl;
cout<<"section Name is : "<<b[i].sec<<endl;
}
}
cout<<"Enter 1 to start program again";
cin>>choice;
}
return 0;}
Exercise 5
Write a program which perform following tasks
a) Read a string from user at runtime (e.g. student name)
b) Convert read value into array of characters
c) Sort the character array based on ASCII values of array elements
d) Display sorted array

#include<iostream>
#include<string.h>
int main()
{
string str=" ";
cout<<"Enter string "<<endl;
cin>>str;
int n, i, j;
n = str.length();
char s[n], a;
for (i=0; i<n; i++)
{s[i]=str[i];
} for (i=0; i<n-1; i++)
{ for (j=i+1; j<n; j++)
{
if (s[i] > s[j])
{
a = s[i];
s[i] = s[j];
s[j] = a;
}
}
}
cout<<s<<endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy