Roll No 150252 Lab-2: Exercise 1
Roll No 150252 Lab-2: Exercise 1
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;
j = i;
tmp = arr[j];
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;
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;
}