1. Pseudo Code Practice Problems:
Listedbelowisabrief explanationof Pseudocode aswell asalistof examplesandsolutions.
Pseudo code
Pseudocode can be brokendownintofive components.
• Variables:
• Assignment:
• Input/output:
• Selection:
• Repetition:
A variable has a name, a data type, and a value. There is a location in memory associated with each
variable. A variable can be called anything or be given any name. It is considered good practice to use
variable namesthatare relevanttothe task at hand.
Assignmentisthe physical actof placinga value intoavariable.Assignmentcanbe shownusing
set= 5;
set= num+ set;
The left side is the variable a value is being stored in and the right side is where the variable is being
accessed. When a variable is assigned a value, the old value is written over with the new value so the old
value is gone. x = 5 does not mean that x is equal to 5; it means set the variable x to have the value 5.
Give x the value 5, make x equal to 5.
Input / Output both deal with an outside source (can be a user or another program) receiving or giving
information. An example would be assuming a fast food restaurant is a program. A driver (user) would
submit their order for a burger and fries (input), they would then drive to the side window and pick up
theirorderedmeal (output.)
• Output– Write / display/print
• Input– Read/ get/ input
Selection construct allows for a choice between performing an action and skipping it. It is our
conditional statements.Selectionstatementsare writtenassuch:
if ( conditional statement)
statementlist
else
statementlist
Repetitionisa construct that allowsinstructionstobe executedmultipletimes(IErepeated).
In a repetitionproblem
• Countis initialized
• Tested
• incremented
Repetitionproblemsare shownas:
while ( conditionstatement)
2. statementlist
Examples
Example 1: Write pseudocode that reads two numbersand multipliesthemtogether andprintout
theirproduct.
Example 2: Write pseudocode thattells a userthat the numberthey enteredisnot a 5 or a 6.
Example 3: Write pseudocode thatperformsthe following:Askauserto entera number. If the number
is between0 and 10, write the word blue.Ifthe numberis between10 and 20, write the word
red. ifthe numberis between20 and 30, write the word green. If it isany othernumber,write
that itis not a correct coloroption.
Example 4: Write pseudocode to print all multiplesof5 between1 and 100 (includingboth1 and100).
Example 5: Write pseudocode thatwill countall the evennumbersup to a userdefinedstopping
point.
Example 6: Write pseudocode thatwill performthe following.
a) Readin 5 separate numbers.
b) Calculate the average of the five numbers.
c) Findthe smallest(minimum) andlargest(maximum)of the five enterednumbers.
d) Write outthe resultsfoundfromstepsband c witha message describingwhattheyare
Homework1: Write pseudocode thatreads inthree numbersandwritesthemall insortedorder.
Homework2: Write pseudocode thatwill calculate arunningsum.A userwill enternumbersthatwill
be addedto the sum and whenanegative numberisencountered,stopaddingnumbersand
write outthe final result.
Solutions
Example 1: Write pseudocode thatreadstwo numbersandmultipliesthemtogetherandprintout their
product.
Pseudocode
Readnum1 , num2
Setmulti to num1*num2
Write multi
Ch code
intnum1, num2,multi;
cin>>num1>>num2;
multi = num1 * num2;
cout<<multi<<endl;
Example 2: Write pseudocode thattellsauser that the numbertheyenteredisnota5 or a 6.
Example 2 Solution1:
PseudoCode: CH code:
3. Readisfive
If(isfive =5)
Write "your numberis5"
Else if (isfive =6)
Write "your numberis6"
Else
Write "your numberisnot5 or 6"
intisfive;
cin>> isfive;
if(isfive==5)
{ cout<<"yournumberis5"; }
else if(isfive ==6)
{ cout<<"yournumberis6"; }
else
{ cout<<"yournumberisnot 5 or 6"; }
Example 2 Solution2:
PseudoCode:
Readisfive
If(isfive =5 or isfive =6)
Write "your numberisa 5 or 6"
Else
Write "your numberisnot5 or 6"
CH code:
intisfive;
cin>> isfive;
if(isfive==5 || isfive ==6)
{ cout<<"yournumberis5 or 6"; }
else
{ cout<<"yournumberisnot 5 or 6"; }
Example 2 Solution3:
PseudoCode:
Readisfive
If(isfive isnot5 andisfive isnot6)
Write "your numberisnot5 or 6"
CH code:
intisfive;
cin>> isfive;
if(isfive!=5 && isfive !=6)
{ cout<<"yournumberisnot 5 or 6"; }
Example 3: Write pseudocode thatperformsthe following:Askauserto entera number.If the number
isbetween0and 10, write the wordblue.If the numberisbetween10and 20, write the word
red.if the numberisbetween20and 30, write the word green.If itisany othernumber,write
that itis not a correct coloroption.
PseudoCode:
Write "Please enteranumber"
Readcolornum
If (colornum>0 andcolornum<= 10)
Write blue
else If (colornum>0 andcolornum<= 10)
Write blue
else If (colornum>0 andcolornum<= 10)
Write blue
else
Write "not a correct color option"
CH code:
intcolornum;
cout<<"Please enteranumber"
cin>> colornum;
if(colornum>0 && colornum<= 10)
{ cout<<"blue"; }
else if(colornum>0 && colornum<= 10)
{ cout<<"blue";}
else if(colornum>0 && colornum<= 10)
{ cout<<"blue"; }
else
{ cout<<"not a correct coloroption" }
4. Example 4: Write pseudocode toprintall multiplesof 5between1and 100 (includingboth1and 100).
PseudoCode:
Setx to 1
While(x <20)
write x
x = x*5
CH code:
intx = 1;
cin>>x;
while(x<20)
{ cout<<x;
x = x*5;
}
Example 5: Write pseudocode thatwill countall the evennumbersuptoa userdefinedstoppingpoint.
For example,saywe wanttosee the first5 evennumbersstartingfrom0.
well,we knowthatevensnumbersare 0,2, 4, etc.
The first5 evennumbersare 0, 2, 4, 6, 8.
The first8 evennumbersare 0, 2, 4, 6, 8 ,10 ,12, 16
Example 5 solution1:
PseudoCode:
Readcount
Setx to 0;
While(x <count)
Set eventoeven+ 2
x = x + 1
write even
CH code:
intx, count,even;
x = 0;
even= 0;
cin>>count;
while(x<count)
{ cout<<even;
even= even+2;
x = x+1;
}
Example 5 solution2:
PseudoCode:
Readcount
Setx to 0;
While(x <count)
Set eventoeven+ 2
x = x + 1
write even
CH code:
intx, count,even;
cout<<"0";
x = 1;
cin>>count;
while(x<count)
{ cout<<x*2;
x = x+1;
}
5. Example 6: Write pseudocode thatwill performthe following.
a) Readin 5 separate numbers.
b) Calculate the average of the five numbers.
c) Findthe smallest(minimum) andlargest(maximum)of the five enterednumbers.
d) Write outthe resultsfoundfromstepsband c witha message describingwhattheyare.
PseudoCode:
Write "please enter5numbers"
Readn1,n2,n3,n4,n5
Write "The average is"
Setavg to (n1+n2+n3+n4+n5)/5
Write avg
If(n1< n2)
Setmax to n2
Else
Setmax to n1
If(n3> max)
Setmax to n3
If(n4> max)
Setmax to n4
If(n5> max)
Setmax to n5
Write "The max is"
Write max
If(n1> n2)
Setminto n2
Else
Setminto n1
If(n3< min)
Setminto n3
If(n4< min)
Setminto n4
If(n5< min)
Setminto n5
Write "The min is"
Write min
CH code:
cout<<"please enter5numbers";
intn1,n2,n3,n4,n5;
cin>>n1>>n2>>n3>>n4>>n5;
intavg = (n1+n2+n3+n4+n5)/5;
cout<<"The average is"<<avg;
intmin,max;
if(n1<n2)
max=n2;
else
max=n1;
if(n3>max)
max=n3;
if(n4>max)
max=n4;
if(n5>max)
max=n5;
cout<<"The max is"<<max;
if(n1>n2)
min=n2;
else
min=n1;
if(n3<min)
min=n3;
if(n4<min)
min=n4;
if(n5<min)
min=n5;
cout<<"The minis"<<min;
Homework1: Write pseudocode thatreads inthree numbersandwritesthemall insortedorder.
7. Homework2: Write pseudocode thatwill calculate arunningsum.A userwill enternumbersthatwill
be addedto the sum and whenanegative numberisencountered,stopaddingnumbersand
write outthe final result.
Pseudo Code:
Readx
Setsum to 0;
While(x >=0)
Set sumto x + sum
Read x
CH code:
intx, sum;
sum= 0;
cin>>x;
while(x>=0)
{ sum = sum + x
cin>>x;
}
C - Basic Introduction
The C is a general-purpose, procedural, imperative computer programming language
developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating
system.
C is the most widely used computer language.
C is a general-purpose high level language that was originally developed by Dennis Ritchie for
the Unix operating system. It was first implemented on the Digital Eqquipment Corporation
PDP-11 computer in 1972.
The Unix operating system and virtually all Unix applications are written in the C language. C
has now become a widely used professional language for various reasons.
• Easy to learn
• Structured language
• It produces efficient programs.
• It can handle low-level activities.
• It can be compiled on a variety of computers.
Facts about C
• C was invented to write an operating system called UNIX.
• C is a successor of B language which was introduced around 1970
• The language was formalized in 1988 by the American National Standard Institue
(ANSI).
• By 1973 UNIX OS almost totally written in C.
• Today C is the most widely used System Programming Language.
• Most of the state of the art software have been implemented using C
8. Why to use C ?
C was initially used for system development work, in particular the programs that make-up the
operating system. C was adoped as a system development language because it produces code that
runs nearly as fast as code written in assembly language. Some examples of the use of C might
be:
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Data Bases
• Language Interpreters
• Utilities
C Program File
All the C programs are writen into text files with extension ".c" for example hello.c. You can use
"vi" editor to write your C program into a file.
This tutorial assumes that you know how to edit a text file and how to write programming
insturctions inside a program file.
C Compilers
When you write any program in C language then to run that program you need to compile that
program using a C Compiler which converts your program into a language understandable by a
computer. This is called machine language (ie. binary format). So before proceeding, make sure
you have C Compiler available at your computer. It comes alongwith all flavors of Unix and
Linux.
If you are working over Unix or Linux then you can type gcc -v or cc -v and check the result.
You can ask your system administrator or you can take help from anyone to identify an available
C Compiler at your computer.
If you don't have C compiler installed at your computer then you can use below given link to
download a GNU C Compiler and use it.
To know more about compilation you can go through this small tutorial Learn HYPERLINK
"http://www.tutorialspoint.com/makefile/index.htm"Makefile.
A C programbasically has the following form:
• P reprocessor Commands
• Functions
• V ariables
9. • Statements & Expressions
• C omments
The following program is written in the C programming language. Open a text file hello.c using vi editor and put the following lines inside
that file.
#include <stdio.h>
int main()
{
/* My first program */
printf("Hello, World! n");
return 0;
}
Preprocessor Commands: T hese commands tells the compiler to do preprocessing before doing actual compilation. Like #include
<stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. You will learn more
about C Preprocessors in C Preprocessors session.
Functions: are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function
which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits.
T his integer value is retured using return statement.
The C Programming language provides a set of built-in functions. In the above example printf()is a C built-in function which is used to print
anything on the screen. Check Builtin functionsection for more detail.
Y ou will learnhow to write yourown functions and use them in Using Function session.
Variables: are used to hold numbers, strings and complex data for manipulation. Y ou will learn in detail about variables in C Variable Types.
Statements & Expressions : Expressions combine variables and constants to create new values . Statements are expressions, assignments,
functioncalls, or control flow statements whichmake up C programs.
Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the
example above. A comment canspan throughmultiple lines.
Note the followings
• C is a case sensitive programming language. It means in C printf and Printf will have different meanings.
• C has a free-formline structure. End of eachC statement must be marked with a semicolon.
• M ultiple statements can be one the same line.
• White Spaces (ie tab space and space bar ) are ignored.
• Statements cancontinue over multiple lines.
C Program Compilation
To compile a C program you would have to Compiler name and program files name. Assuming your compiler's name is cc and progra m file
name is hello.c, give following command at U nix prompt.
$cc hello.c
This will produce a binary file called a.out and an object file hello.o in your current directory. Here a.out is your first program which you will
run at Unix prompt like any other system program. If you don't like the name a.out then you can produce a binary file with your own name by
using -o option while compiling C program. See an example below
$cc -o hello hello.c
Now you will get a binary with name hello. Execute this program at Unix prompt but before executing / running this program make sure that
it has execute permissionset. If youdon't know what is execute permissionthen just follow these two steps
$chmod 755 hello
$./hello
This will produce following result
Hello, World
C ongratulations!! youhave writtenyour first programin "C". Now believe me its not difficult to learn "C".