You are on page 1of 14

Course Work 1

Name : Jobin Solomon


Student No : 2622588
Marker :
Unit : Engineering Computing
Unit code : ECS_1_101
Question 1 a)

Source Code

/* This program is used to input two numbers and display them in descending order.
It has been written by Jobin Solomon, Student no 2622588 (LSBU) on 7/03/09.
It was written using Microsoft Visual Studio 2008 Express Edition.
*/

#include <iostream> //pre processor directive to include header files

using namespace std; //to establish standard syntax

void main() //main function definition

{
int num1,num2; //initialization of required variables.

cout <<"\n Please enter first number"<<endl; //user prompt to enter first variable

cin>>num1; //inputing first variable

cout<<"\n Please enter second number"<<endl; //user prompt to enter second variable

cin>>num2; //inputing second variable

if (num1>num2) //checking which number is larger


//the below statement will print out the numbers in descending order
cout << "\n Larger to smaller number is:\t" << num1<<" \t"<< num2 <<endl;

else //the else statement is used if the if condition is not true.


//the below statement will print the numbers in descending order.
cout << "\nlarger to smaller number is:\t" << num2 <<"\t" <<num1 <<endl;

}//End of program.
Output Screen Shots
Question 1 b) In this question I have been asked to comment on the given code's out put.

# include <iostream>

using namespace std ;

int main ()

{
int a=1, b=1, c ;

if (c = (a-b))

cout << "The value of c is :" << c;

return 0 ;

Answer : Th e if s tat ement w orks o n logic, it w il l execute the code in the con t in u in g
lin e o r s eg men ted code (bracket ed code) provided the boolean express ion in th e
arg u men t o f the if s tat ement re turns a logic valu e of true w hos e numer ica l
eq u iv alen t is 1. If the argumen t is zero than it w il l not exe cute the code con t in u in g
b elo w an d w ill s kip i t. In the above ques tion the argum ent is c = (a- b), in th is
ex p r es s io n w e have us ed the as s ignmen t opera tor ' =' and not th e comp ar is o n
o p er a to r ' =' w hich me ans c w ill be ass ign ed the value of a- b which is zer o . Th is
me an s th e if condi tion is not val id and henc e it s kips the cout s tat ement. H en ce
th is p r o g r am w ill have no outpu t and w ill be a bl ank s creen.
Question 2 a)

Source Code

/* This program is designed to input a name and print out the name in a sentence.
It is been written by Jobin Solomon, Student no 2622588(LSBU) on 7/03/09.
It has been written using Microsoft Visual Studio 2008 Express edition.
*/

#include <iostream> //pre processor directive

using namespace std; // establishing general syntax standard

void main()//main function definition


{
char name[10]; // Declaring string array

cout<<"\nPlease enter your name : "<<endl;//user prompt to enter name

cin>>name; //inputing name into string

cout<<"\nYour name is :\t"<<name<<endl;//printing name in a sentence on screen


}//End of program
Output screen shots
Question 2 b)

Source Code

/* This program is designed to input an integer type number and a float type number and
calculate the total of the two.
It is been written by Jobin Solomon, Student no 2622588(LSBU) on 7/03/09.
It has been written using Microsoft Visual Studio 2008 Express edition.
*/

#include <iostream> //pre processor directive

using namespace std; //establishing standard syntax

void main ()// main function definition

{
int number1; // Declaring int type variable.

float number2, total;// declaring float type variables.

cout<<"\n\nPlease enter an integer :";//user prompt for integer

cin>>number1;//inputing integer

cout<<"\n\nPlease enter a decimal number :";//user prompt for decimal

cin>>number2;//inputing decimal.

total = number1 +number2;//calculation of total of both numbers.

cout<<"\n\nThe total is "<<total<<endl;//printing on screen the total value.

}//End of program
Output screen shots
Question 2 c)

Source Code

/* This program is designed to input any given inches and convert to centimetres
and print result.
It is been written by Jobin Solomon, Student no 2622588(LSBU) on 7/03/09.
It has been written using Microsoft Visual Studio 2008 Express edition.
*/

#include <iostream> //pre processor directive

using namespace std;//establishing standard syntax

void main()//main function definition

{
int centimetres, inches;//initializing required variables.

cout<<"\n\nPlease enter a value in inches : ";//user prompt to enter inches

cin>>inches;//inputing inches.

centimetres = inches * 2.5;//converting inches to centimetres and store in variable

cout<<"\n\nThe value in centimetres is "<<centimetres<<endl;//printing result onto screen

}
Output screen shots
Question 3

Source Code

/* This program is designed to calculate the capacitative reactance of a


given capacitor. The frequency is given as 10Khz and the capacitance is
given as 1 microFarad.
This program is written by Jobin Solomon, Student no 2622588 on 7/03/09.
It was written in Microsoft Visual Studio 2008 Express edition.
*/

#include <iostream> //pre processor directive.

using namespace std;// establishing standard syntax.

void main()//main function definiton.

#define PI 3.14//defining the constant PI.

{
float c=1e-6,xc;//initializing the float variables.

int f=10e3;//initializing the integer variable.

xc = 1/(2*PI*c*f);//calculation of capacitative reactance.

cout<<"\n\nXc is "<<xc<<" ohms"<<endl;//Print out the result on screen.

}//End of program.
Output screen shots
Question 4

Source code

/* This program is designed to input a mark and give a corresponding grade.


This program is written by Jobin Solomon, Student no 2622588 (LSBU) on 7/03/09.
It was written in Microsoft Visual Studio 2008 Express edition.
*/

#include <iostream> //pre processor directive

using namespace std;//establishing standard syntax

void main ()//main function declaration.

{
float mark;//initialization of variable to store mark.

cout << "\n\nPlease enter your test mark to obtain your grade :";//user prompt for marks

cin>>mark;//inputing marks.

if ((mark<=100)&&(mark>=40))//first condition checking validity of marks


{

if ((mark>=70)&&(mark <=100))//checking if marks are grade A

cout << " \nYour grade is A"<<endl;//printing result onto screen

if ((mark>=60)&&(mark <=69))//checking if marks are grade B

cout << " \nYour grade is B"<<endl;//printing result onto screen

if ((mark>=50)&&(mark <=59))//checking if marks are grade C

cout << " \nYour grade is C"<<endl;//printing result onto screen

if ((mark>=40)&&(mark <=49))//checking if marks are grade D

cout << " \nYour grade is D"<<endl;//printing result onto screen

}
else
cout << "\nInvalid marks entered \n";//error message for invalid marks

}//End of program
Output screen shots

You might also like