Bubble Sort-Programming For Engineers II-Quiz Solution, Exercises of Programming for Engineers

Sir Prachur Sharma took this quiz in class of Programming for Engineers course at Bhagwant University Rajasthan. It includes: Bubble, Sort, Algorithm, Upward, Top, Array, Rising, Increasing, Order, Pair, Calues, Identical, Compared

Typology: Exercises

2011/2012

Uploaded on 07/25/2012

anasuya
anasuya ๐Ÿ‡ฎ๐Ÿ‡ณ

4

(9)

85 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz # 3
(Programming-ll-EE112)
Time: 10 minutes Date: 26.02.09
Marks: 10
1. (Bubble Sort) In the bubble sort algorithm, smaller values gradually "bubble" their way
upward to the top of the array like air bubbles rising in water, while the larger values sink
to the bottom. The bubble sort makes several passes through the array. On each pass,
successive pairs of elements are compared. If a pair is in increasing order (or the values
are identical), we leave the values as they are. If a pair is in decreasing order, their values
are swapped in the array. Write a program that sorts an array of 10 integers using bubble
sort.
#include<iostream.h>
void main()
{
int a[10];
cout<<"Enter the elements\n";
for(int i=0;i<10;i++)
{
cin>>a[i];
}
for ( i=0;i<10;i++)
{
for (int j=i;j<10;j++)
{
if (a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for (i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
}
docsity.com
pf3
pf4

Partial preview of the text

Download Bubble Sort-Programming For Engineers II-Quiz Solution and more Exercises Programming for Engineers in PDF only on Docsity!

Quiz # 3

(Programming-ll-EE112)

Time: 10 minutes Date: 26.02.

Marks: 10

1. (Bubble Sort) In the bubble sort algorithm, smaller values gradually "bubble" their way

upward to the top of the array like air bubbles rising in water, while the larger values sink

to the bottom. The bubble sort makes several passes through the array. On each pass,

successive pairs of elements are compared. If a pair is in increasing order (or the values

are identical), we leave the values as they are. If a pair is in decreasing order, their values

are swapped in the array. Write a program that sorts an array of 10 integers using bubble

sort.

#include<iostream.h>

void main()

int a[10]; cout<<"Enter the elements\n"; for(int i=0;i<10;i++) { cin>>a[i]; }

for ( i=0;i<10;i++) { for (int j=i;j<10;j++) { if (a[i]>a[j]) { int temp=a[i]; a[i]=a[j]; a[j]=temp; }

} }

for (i=0;i<10;i++) { cout<<a[i]<<endl;

}

2. Create a structure to specify data on students given below: Roll number, Name,

Department, Course, and Year of joining

Assume that there are not more than 500 students in the University.

  • (a) Write a function to print names of all students who joined in a

particular year.

  • (b) Write a function to print the data of a student whose roll number is

given.

struct student { int roll; char *name; char *department; int year; int courses;

//this function will check either //students enterd are not more than 500// void Enter_data_of_student();

//to print data of student whos roll no is given// void print_data_student(int roll);

//To print names of all students who joined in particular year// void print_names(int); };

โ€˜โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™โ€™

#include"structfile.h" #include<iostream.h> static int r=1; static int y=1990; int check=0; void student::print_names(int yr) { if(yr==year) cout<<name<<endl; } void student::Enter_data_of_student() { //cout<<"Enter name:"; //cout<<"Enter Roll:"; //cout<<"Enter year:"; //cout<<"Enter dep: "; //cout<<"Enter course";

name="my_name"; roll=r; r++; department="std_deprtment";

int rr=0; cout<<"Enter roll no to print student data :"; cin>>rr; if(rr>std_no && rr<=0) cout<<"Error!!:-"<<endl; else { for(int k=0;k<std_no;k++) students[k].print_data_student(rr); }

}