cs201 assignment solution

This post contains cs201 assignment solutution

Assignment’s Objectives:

To enable students to write, compile and execute a program in Dev C++. Moreover to familiarize students with  the concepts of:

  • Loops
  • Switch, break, continue
  • Functions
  • Arrays
  • Pointers

Write a program which defines three functions:

  • genRandNum()
  • findMinNum()
  • swapValue()

Assignment:

genRandNum(): This function will generate 5 random numbers in range 1-100 and store these numbers in array and print array on console.

Now start sorting these numbers in ascending order (lower to higher) by following the given steps:

  1. findMinNum(): This function will search smallest number in unsorted part of array.

2 .If leftmost number is the smallest number in unsorted array then no need to swap it otherwise                     swap the smallest number with left most unsorted number using swapValue() function. Now                       this number is fully sorted

3 :Repeat the above 2 steps until the whole array get sorted.

Now display the sorted array as given in the screenshot.

 

Note: Use srand() with rand() library function to generate unique random numbers each time we run the program because rand() function will generate same number each time.

Assignment Submission Instructions

You have to submit only.cpp file on the Assignments interface of CS201 at VULMS. Assignment submitted in any other format will not be accepted and will be graded zero marks.

cs201 assignment solution

Complete code:

#include <iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

void genRandNum(int *array)


{
cout<<" Random Numbers are : ";
srand(time(NULL));
for(int i=0; i<5; i++)

{ 
int x=rand()%110+1;
array [i]=x;
cout<<x<<" ";

}

}
void swapValue(int array[],int n1,int n2)
{
int swp=array[n1];
array [n1]=array [n2];
array[n2]=swp;
}

void findMinNum(int array [],int size)
{
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-i-1;j++)
{
if(array [j]>array [j+1])
{
swapValue(array, j,j+1);
}
}
}
}
main()
{
int array [5];
genRandNum(array);
findMinNum(array,5);
cout<<endl;
cout<<"Sorted Random Numbers are: ";
for(int i=0; i<5;i++)
{
cout<<array [i]<<" ";
}

system("pause");
}

TO Run  online click on following button

RUN

 

NOTE : If you are facing any difficulty  please let me know in comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *