11-29-2018, 08:27 PM
CS201 - Introduction to Programming
Assignment 1 Solution
Semester: Autumn 2018
Save the following code in devC++ as .cpp and press F11 to compile and run.
Assignment 1 Solution
Semester: Autumn 2018
Save the following code in devC++ as .cpp and press F11 to compile and run.
Code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void swapValue(int *val1, int *val2) {
int temp;
temp = *val1;
*val1 = *val2;
*val2 = temp;
}
void findMinNum(int number[]) {
int temp, i, j;
for(i = 0; i < 5; i++) {
for(j=i+1; j<5; j++) {
if(number[i] > number[j]) {
swapValue(&number[i],&number[j]);
}
}
}
cout <<"\nSorted Random Numbers are: ";
for(int i =0; i <5; i++) {
cout<< number[i] << '\t';
}
}
void genRandNum(int num []) {
srand( (unsigned)time(0) );
cout <<"\nRandom Numbers are: ";
for(int i=0; i < 5; i++) {
num[i] = rand()%100;
cout << num[i] << "\t";
}
}
main() {
cout <<"\t CS201 Indroduction to Programing" <<endl;
int random_num[5];
genRandNum(random_num);
cout <<endl;
findMinNum(random_num);
cout <<endl<<endl;
system("pause");
}