OOP – ASSIGNMENT I

Lab Manual Download: Write a program in C++ to sort the numbers in an array using separate functions for read, display, sort and swap. The objective of this assignment is to learn the concepts of input, output, functions, call by reference in C++

ASSIGNMENT NO 1

 Title: Demonstration of Basics of C++

Objectives: To learn input, output functions in C++ and concept of call by reference.

Problem Statement:  Write a program in C++ to sort the numbers in an array using separate functions for read, display, sort and swap. The objective of this assignment is to learn the concepts of input, output, functions, call by reference in C++

Outcomes: 1) Students will be able to demonstrate use of input and output functions of C++

2) Students will be able to demonstrate call by reference in C++

Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

 Software requirements:  64 bit Linux/Windows Operating System, g++ compiler or TurboC

 Theory:

C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen, the keyboard or a file. A stream is an entity where a program can either insert or extract characters to/from. There is no need to know details about the media associated to the stream or any of its internal specifications. All we need to know is that streams are a source/destination of characters, and that these characters are provided/accepted sequentially (i.e., one after another).
The standard library defines a handful of stream objects that can be used to access what are considered the standard sources and destinations of characters by the environment where the program runs:

stream description
cin standard input stream
cout standard output stream
cerr standard error (output) stream
clog standard logging (output) stream

Standard output (cout): On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout.
For formatted output operations, cout is used together with the insertion operator, which is written as << (i.e., two “less than” signs). The << operator inserts the data that follows it into the stream that precedes it

Example:

cout<”Hello”; //prints string Hello
cout<<hello; //prints value of variable hello

Standard input (cin): In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin.
For formatted input operations, cin is used together with the extraction operator, which is written as >> (i.e., two “greater than” signs). This operator is then followed by the variable where the extracted data is stored.

Example:

int marks; //declares variable
cin>> marks; //accepts integer value from keyboard

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.

// function definition to swap the values.
void swap(int &x, int &y) {
   int temp;
   temp = x; /* save the value at address x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */
    return;
}

 Conclusion: In this assignment, we have studied arrays in Java and demonstrated their use in sorting integer and string array.

Roll No. Name of Student Date of Performance Date of Assessment Grade Sign of Student Sign of Faculty

PROGRAM

/*
 * Write a program in C++ to sort the numbers in an array
 * using separate functions for read, display, sort and swap.
 * The objective of this assignment is to learn the concepts of input, output,
 * functions, call by reference in C++
 */

#include<iostream>
using namespace std;
//#include<conio.h>
#define SIZE 10
class Sort
{
private:

 int elements[SIZE];
public:
 void read();
 void display();
 void sorting();
 void swap(int &a,int &b);
};
void Sort::read()
{
 cout<<"Enter "<<SIZE<<" elements in array for sorting";
 for(int i=0;i<SIZE;i++)
 cin>>elements[i];
}
void Sort::display()
{
 for(int i=0;i<SIZE;i++)
 cout<<elements[i]<<"\t";
}
void Sort::sorting()
{
 for(int i=0;i<SIZE;i++)
 for(int j=i;j<SIZE;j++)

 if(elements[i]>elements[j])
 swap(elements[i],elements[j]) ;
}
void Sort::swap(int &a, int &b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;

}
int main()
{
 Sort s;
 s.read();
 cout<<"\n********************Array before sorting*******************\n";
 s.display();
 s.sorting();
 cout<<"\n********************Array after sorting*******************\n";
 s.display();
 // getch();
 return 0;
}

OUTPUT:

Enter 10 elements in array for sorting

1   3   -4   -7   -22   33   66   44   34   21
********************Array after sorting*******************
1   3   -4   -7   -22   33   66   44   34   21

********************Array before sorting*******************
-22   -7   -4   1   3   21   33   34   44   66

Leave a Reply

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