0% found this document useful (0 votes)
119 views16 pages

Unit 7

The document provides examples of C++ programs to demonstrate the versatility of C++ for various applications. It includes programs to check if a number is even or odd, calculate grades based on marks, list student details, and perform bubble sort. Sample outputs are provided for each program. The objectives are to design and write C++ programs and apply C++ to different domains. Activities with sample inputs and outputs are included to test understanding.

Uploaded by

ijaisa77
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
119 views16 pages

Unit 7

The document provides examples of C++ programs to demonstrate the versatility of C++ for various applications. It includes programs to check if a number is even or odd, calculate grades based on marks, list student details, and perform bubble sort. Sample outputs are provided for each program. The objectives are to design and write C++ programs and apply C++ to different domains. Activities with sample inputs and outputs are included to test understanding.

Uploaded by

ijaisa77
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16

E3062/7/1

Writing C++ Program

UNIT 7

Writing C++ Program

OBJECTIVES

General Objective : To write C ++ program

Specific Objectives : At the end of the unit you will be able to :

 design and write a C++ program


 use C++ program on various applications
E3062/7/2
Writing C++ Program

INPUT

7.0 Introduction

In this unit , we will analyze some set of


sample programs to demonstrate C++
versatility and power in various kind of
applications. This will show the diversity of
C++ program in the development of software
applications.

7.1 Simple programming

7.1.1 Even odd


/* This program evaluates the number input is an even or odd number */
#include <iostream.h>
void main ( )
{
int n;
cout<< “ Enter a number”;
cin>>n;
if (( n %2==0) && ( n!=0))
E3062/7/3
Writing C++ Program

cout<<”it is an even number”;


else
cout<<”it is an odd number”;
}
The output would be:
Sample 1:
Enter a number : 12
It is an even number
Sample 2:
Enter a number : 5
It is an odd number

7.1.2 Grade

/* This program accepts the mark of 5 subjects, calculates and outputs the
average and its appropriate grade */
#include<iostream.h>
void main( )
{
float a,b,c,d,e,f ;

cout<<” Enter 5 mark values: ”;


cin>>a>>b>>c>>d>>e;
f=(a+b+c+d+e)/5;

cout<<f<<endl;
if((f>=0) && (f<50))
{
cout<< “Grade is F”;
}
else if ((f>=50) && (f<62))
E3062/7/4
Writing C++ Program

{
cout<< “Grade is D-”;
}
else if ((f>=62) && (f<66))
{
cout<< “Grade is D”;
}
else if ((f>=66) && (f<69))
{
cout<< “Grade is D+”;
}
else if ((f>=69) && (f<72))
{

cout<< “Grade is C-”;


}
else if ((f>=72) && (f<76))
{
cout<< “Grade is C”;
}
else if ((f>=76) && (f<79))
{
cout<< “Grade is C +”;
}

else if ((f>=79) && (f<82))


{
cout<< “Grade is B-”;
}
else if ((f>=82) && (f<86))
{
cout<< “Grade is B”;
E3062/7/5
Writing C++ Program

}
else if ((f>=86) && (f<89))
{
cout<< “Grade is B+”;
}
else if ((f>=89) && (f<92))
{

cout<< “Grade is A-”;


}

else if ((f>=92) && (f<100))


{
cout<< “Grade is A”;
}
}

The output would be :

Enter 5 mark values: 76 79 87.5 93 99


86.9
Grade is B+
E3062/7/6
Writing C++ Program

Activity 7A

TEST YOUR UNDERSTANDING BEFORE YOU CONTINUE WITH THE


NEXT INPUT…!
7.1 Find out the output.

Positive Negetive
/* This program evaluates the number keyed in is either a positive or negetive
value */
#include <iostream.h>
void main ( )
{
int n;
cout<< “Enter Value:”;
cin>>n
if((n>0) && (n!=0))
cout<<n<< “ n is a positive value”;
else
cout<<n<< “ n is a negetive value”;
}
E3062/7/7
Writing C++ Program

Feedback To Activity 7A

7.1.

The output would be:


Sample 1:
Enter a value: 30
30 is a positive value
Sample 2:
Enter a value : -29
-29 is a negetive value
E3062/7/8
Writing C++ Program

INPUT

7.1.3 Student List

/* This program prompt 3 students details and list them accordingly( name,
id, and major) */
#include<iostream.h>
#include<string.h>
int i;
class student
{
private : string name;
int idno;
string major;
public : void insert( )
{
cout<< “\nEnter details of student:”<<endl;
cin>>name>>idno>>major;
}
void display ( )
{
cout<< “\n”<<name<< “ ” <<idno<< “ ”<<major<<endl;
}
}

void main ()
E3062/7/9
Writing C++ Program

{
student s[ 4 ];
for (i=1;i<4;i++)
s[ i ].insert();
cout<< “\n\nList of students”<<endl;
for( i=1;i<4; i++)
s[ i ] .display();
}

The output would be:

Enter details of student:


Jason
123
Programming
Enter details of student:
Jenny
124
Multimedia
Enter details of student:
Susie
125
Networking

List of students

Jason 123 Programming


Jenny 124 Multimedia
Susie 125 Networking
E3062/7/10
Writing C++ Program

7.1.4 Bubble Sort


/* This sample program illustrates how to write a Bubble Sort */
#include<iostream.h>
using namespace std;
/* Set the total number of integers that will sorted. */
#define MAX 25
int main()
{

int a[MAX]; /* Allocate an array to hold MAX integers. */


int I; /* This will be used an index into the array.*/
int done; /* This is a flag used to indicate when the array of numbers is
completely sorted and that no other work needs to be done */
/* Initialise the array with random values from 0 to 99 */
for (i=0; i<MAX;i++)
a[ i ]= rand( ) % 100;
/* Display original array.*/
cout<<” ORIGINAL ARRAY\n”;
for ( i=0; i< MAX ; i++)
cout<<a[ i ] << “ ”;
cout<< “\n\n”;
do {
/* Start by assuming you are done.*/
done = 1;
/* Now go through the array checking that everything is already sorted.*/
for ( i=0;i<MAX-1; i++) {
/* If there are numbers out of order then swap them and declare the job
of sortingto be “not done”. */
if( a[ i ] > a[i+1] {
int tmp; /* To perform the swap you need to have temporary storage
area for of the integers.
E3062/7/11
Writing C++ Program

Look at this section carefully and try to understand


Why you need to have the “tmp” variable. */
Tmp = a[ i ];
a[ i ] = a[ i+1];
a[i+1] = tmp;
done = 0
}
}
} while ( !done);
/* Display sorted array */
cout<< “ SORTED ARRAY\N”;
for ( i=0;i<MAX; i++)

cout<<a[i] << “ ”;
cout<< “ \n”;
return 0;
}

The output would be:

OROGINAL ARRAY
41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95
42 27 36 91 4 2 53 92

SORTED ARRAY
0 2 4 5 24 27 27 34 36 41 42 45 53 58 61 62 64
67 69 78 81 91 91 92 95
E3062/7/12
Writing C++ Program

Activity 7B

TEST YOUR UNDERSTANDING BEFORE YOU CONTINUE WITH THE


NEXT INPUT…!

7.2. Write a program that checks whether the character you type is a vowel or not
with the following output

Sample 1 for non vowel character


Enter a Character: t
It is a non – vowel character
Sample 2 for vowel character
Enter a Character: a
It is a vowel character
E3062/7/13
Writing C++ Program

Feedback To Activity 7B

7.2.
/* This program checks whether the character typed is vowel or not */

#include<iostream.h>
using namespace std;
void main ()
{
char c;
cout<< “ Enter a character:”;
cin>>c;
switch (c)
{
case ‘a’:cout<< “It is a vowel character”;
break;
case ‘e’:cout<< “It is a vowel character”;
break;
case ‘i’:cout<< “It is a vowel character”;
break;
case ‘o’:cout<< “It is a vowel character”;
break;
case ‘u’:cout<< “It is a vowel character”;
break;
default:cout<< “It is a non – vowel character”;
}
}
E3062/7/14
Writing C++ Program

1.

SELF-ASSESSMENT

You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 2
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.

Question 7-1

Write a program that finds the square of the number entered with the following
format output:

Enter a value: number


The square of the number entered is answer

Question 7-2

Write a program that accepts three values and selects the greatest number with the
following format output:

Enter 3 different numbers: 3 different values of numbers


One of the number selected is the greatest
E3062/7/15
Writing C++ Program

Feedback To Self-Assessment

Have you tried the question????? If “YES”, check your answer now:

Question 7.1
/* This program finds the square of the number entered*/

#include<iostream.h>
using namespace std;
void main( )
{
int a;
cout<< “Enter a value:”;
cin>>a;
cout<<endl<< “The square of ” <<a<< “ is”<<a*a;
}

Question 7.2
/* This program accepts three values and selects the greatest
number */
#include<iostream.h>
using namespace std;
void main ( )
{
int a,b,c;
E3062/7/16
Writing C++ Program

cout<< “ Enter 3 values:” ;


cin>>a>>b>>c;
if (( a>b) && ( b>a))
{
cout<<a<< “ is greatest”;
}
else if ((b>c) && (b>a))
{
cout<<b<< “ is the greatest”;
}
else
{
cout<<c<< “is the greatest”;
}
}

CONGRATULATIONS!!!!…..
May success be with you
always….

You might also like