SlideShare uma empresa Scribd logo
1 de 9
Baixar para ler offline
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)
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:
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" }
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;
}
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.
Homework1 solution1:
PseudoCode:
Readnum1, num2,num3
If (num1 < num2)
If(num2< num3)
Write num1 ,num2, num3
Else
If(num3< num1)
Write num3,num1, num2
Else
Write num1,num3, num2
else
If(num1< num3)
Write num2 ,num1, num3
Else
If(num3< num2)
Write num3,num2, num1
Else
Write num2,num3, num1
CH code:
intnum1, num2,num3;
cin>> num1>>num2>>num3;
if (num1 < num2)
{ if(num2< num3)
{ Cout<< num1<<" "<<num2<<" "<<num3; }
else
{ if(num3< num1)
{ Cout<< num3<<" "<<num2<<" "<<num2; }
else
{ Cout<< num1<<" "<<num3<<" "<<num2; }
}
}
else
{ If(num1< num3)
{ Cout<< num2<<" "<<num1<<" "<<num3; }
else
{ If(num3< num2)
{ Cout<< num3<<" "<<num2<<" "<<num1; }
else
{ Cout<< num2<<" "<<num3<<" "<<num1; }
}
}
Homework1 solution2:
PseudoCode:
Readnum1, num2,num3
If (num1 < num2 < num3)
Write num1 , num2, num3
else If (num1< num3 < num2)
Write num1 , num2, num3
else If (num2< num1 < num3)
Write num2 , num1, num3
else If (num2< num3 < num1)
Write num2 , num3, num1
else If (num3< num1 < num2)
Write num3 , num1, num2
else If (num3< num2< num1)
Write num3 , num2, num1
CH code:
intnum1, num2;
cin>> num1>>num2>>num3;
if(num1< num2 && num2 < num3&& num1 < num3)
{ cout<<num1<<" "<<num2<<" "<<num3; }
else if(num1< num2 && num2 > num3&& num1 < num3)
{ cout<<num1<<" "<<num3<<" "<<num2; }
else if(num1> num2 && num2 < num3&& num1 < num3)
{ cout<<num2<<" "<<num1<<" "<<num3; }
else if(num1> num2 && num2 < num3&& num1 > num3)
{ cout<<num2<<" "<<num3<<" "<<num1; }
else if(num1< num2 && num2 > num3&& num1 > num3)
{ cout<<num3<<" "<<num1<<" "<<num2; }
else if(num1> num2 && num2 > num3&& num1 > num3)
{ cout<<num3<<" "<<num2<<" "<<num1; }
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
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
• 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".

Mais conteúdo relacionado

Mais procurados

Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsPrabu U
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in PythonSumit Satam
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
Data types in python
Data types in pythonData types in python
Data types in pythonRaginiJain21
 
Conditionalstatement
ConditionalstatementConditionalstatement
ConditionalstatementRaginiJain21
 
DataFrame in Python Pandas
DataFrame in Python PandasDataFrame in Python Pandas
DataFrame in Python PandasSangita Panchal
 

Mais procurados (20)

Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
Exception handling
Exception handlingException handling
Exception handling
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, Strings
 
Enums in c
Enums in cEnums in c
Enums in c
 
C if else
C if elseC if else
C if else
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
Array and string
Array and stringArray and string
Array and string
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Nested loops
Nested loopsNested loops
Nested loops
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
DataFrame in Python Pandas
DataFrame in Python PandasDataFrame in Python Pandas
DataFrame in Python Pandas
 

Destaque

Change management for leaders to achieve business competitive
Change management for leaders to achieve business competitiveChange management for leaders to achieve business competitive
Change management for leaders to achieve business competitiveRidwan Ibrahim
 
Result assessment re alignment culture report Danareksa
Result assessment re alignment culture report DanareksaResult assessment re alignment culture report Danareksa
Result assessment re alignment culture report DanareksaRidwan Ibrahim
 
Avoid the resistance on change management
Avoid the resistance on change managementAvoid the resistance on change management
Avoid the resistance on change managementRidwan Ibrahim
 
Investigating the flexibility of selfish herd theory under varying conditions...
Investigating the flexibility of selfish herd theory under varying conditions...Investigating the flexibility of selfish herd theory under varying conditions...
Investigating the flexibility of selfish herd theory under varying conditions...Andy Gray
 
Leadership quality of abdul kalam
Leadership quality of abdul kalamLeadership quality of abdul kalam
Leadership quality of abdul kalamKeval Goyani
 
Implementation Strategy for Research and Community Service of University
Implementation Strategy for Research and Community Service of UniversityImplementation Strategy for Research and Community Service of University
Implementation Strategy for Research and Community Service of UniversityRidwan Ibrahim
 
Prevention Strategy for Money Laundering
Prevention Strategy for Money LaunderingPrevention Strategy for Money Laundering
Prevention Strategy for Money LaunderingRidwan Ibrahim
 
Scentroid Toms Capabilities ENGLISH
Scentroid Toms Capabilities ENGLISHScentroid Toms Capabilities ENGLISH
Scentroid Toms Capabilities ENGLISHSidarta Medina
 
Alignment HRBP with ISO 41000-2015 to Achieve Business on Globalization
Alignment HRBP with ISO 41000-2015 to Achieve Business on GlobalizationAlignment HRBP with ISO 41000-2015 to Achieve Business on Globalization
Alignment HRBP with ISO 41000-2015 to Achieve Business on GlobalizationRidwan Ibrahim
 
Jimbo | Pitch Deck
Jimbo | Pitch DeckJimbo | Pitch Deck
Jimbo | Pitch DeckJimbo
 
Change management towards sustainable business growth (aligned with ISO 41000...
Change management towards sustainable business growth (aligned with ISO 41000...Change management towards sustainable business growth (aligned with ISO 41000...
Change management towards sustainable business growth (aligned with ISO 41000...Ridwan Ibrahim
 
Hotel Vyas & Nirvana Yoga
Hotel Vyas & Nirvana YogaHotel Vyas & Nirvana Yoga
Hotel Vyas & Nirvana YogaYogi Vyas
 
HVS-UnderstandingOnlineDistributionChannels
HVS-UnderstandingOnlineDistributionChannelsHVS-UnderstandingOnlineDistributionChannels
HVS-UnderstandingOnlineDistributionChannelsJuan Pablo Duran
 
Talent Developing Strategy For Human Capital To Achieve ROI
Talent Developing Strategy For Human Capital To Achieve ROITalent Developing Strategy For Human Capital To Achieve ROI
Talent Developing Strategy For Human Capital To Achieve ROIRidwan Ibrahim
 

Destaque (20)

Change management for leaders to achieve business competitive
Change management for leaders to achieve business competitiveChange management for leaders to achieve business competitive
Change management for leaders to achieve business competitive
 
Result assessment re alignment culture report Danareksa
Result assessment re alignment culture report DanareksaResult assessment re alignment culture report Danareksa
Result assessment re alignment culture report Danareksa
 
Avoid the resistance on change management
Avoid the resistance on change managementAvoid the resistance on change management
Avoid the resistance on change management
 
Investigating the flexibility of selfish herd theory under varying conditions...
Investigating the flexibility of selfish herd theory under varying conditions...Investigating the flexibility of selfish herd theory under varying conditions...
Investigating the flexibility of selfish herd theory under varying conditions...
 
Leadership quality of abdul kalam
Leadership quality of abdul kalamLeadership quality of abdul kalam
Leadership quality of abdul kalam
 
Implementation Strategy for Research and Community Service of University
Implementation Strategy for Research and Community Service of UniversityImplementation Strategy for Research and Community Service of University
Implementation Strategy for Research and Community Service of University
 
Resume
ResumeResume
Resume
 
Resume 150927
Resume 150927Resume 150927
Resume 150927
 
Prevention Strategy for Money Laundering
Prevention Strategy for Money LaunderingPrevention Strategy for Money Laundering
Prevention Strategy for Money Laundering
 
Scentroid Toms Capabilities ENGLISH
Scentroid Toms Capabilities ENGLISHScentroid Toms Capabilities ENGLISH
Scentroid Toms Capabilities ENGLISH
 
Driver Hire Australia Brochure
Driver Hire Australia BrochureDriver Hire Australia Brochure
Driver Hire Australia Brochure
 
Alignment HRBP with ISO 41000-2015 to Achieve Business on Globalization
Alignment HRBP with ISO 41000-2015 to Achieve Business on GlobalizationAlignment HRBP with ISO 41000-2015 to Achieve Business on Globalization
Alignment HRBP with ISO 41000-2015 to Achieve Business on Globalization
 
Resume shruti kapoor- ASMs
Resume shruti kapoor- ASMsResume shruti kapoor- ASMs
Resume shruti kapoor- ASMs
 
DISC-SIDARTA_MEDINA
DISC-SIDARTA_MEDINADISC-SIDARTA_MEDINA
DISC-SIDARTA_MEDINA
 
Jimbo | Pitch Deck
Jimbo | Pitch DeckJimbo | Pitch Deck
Jimbo | Pitch Deck
 
Change management towards sustainable business growth (aligned with ISO 41000...
Change management towards sustainable business growth (aligned with ISO 41000...Change management towards sustainable business growth (aligned with ISO 41000...
Change management towards sustainable business growth (aligned with ISO 41000...
 
Resume for 2016
Resume for 2016Resume for 2016
Resume for 2016
 
Hotel Vyas & Nirvana Yoga
Hotel Vyas & Nirvana YogaHotel Vyas & Nirvana Yoga
Hotel Vyas & Nirvana Yoga
 
HVS-UnderstandingOnlineDistributionChannels
HVS-UnderstandingOnlineDistributionChannelsHVS-UnderstandingOnlineDistributionChannels
HVS-UnderstandingOnlineDistributionChannels
 
Talent Developing Strategy For Human Capital To Achieve ROI
Talent Developing Strategy For Human Capital To Achieve ROITalent Developing Strategy For Human Capital To Achieve ROI
Talent Developing Strategy For Human Capital To Achieve ROI
 

Semelhante a Pseudo code practice problems+ c basics

Semelhante a Pseudo code practice problems+ c basics (20)

Pseudo_Code_Practice_Problems.pdf
Pseudo_Code_Practice_Problems.pdfPseudo_Code_Practice_Problems.pdf
Pseudo_Code_Practice_Problems.pdf
 
Pseudocode-cheat-sheet-A4.docx
Pseudocode-cheat-sheet-A4.docxPseudocode-cheat-sheet-A4.docx
Pseudocode-cheat-sheet-A4.docx
 
algorithm
algorithmalgorithm
algorithm
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
2 2 rational numbers trout09
2 2 rational numbers trout092 2 rational numbers trout09
2 2 rational numbers trout09
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
C code examples
C code examplesC code examples
C code examples
 
C important questions
C important questionsC important questions
C important questions
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
10 template code program
10 template code program10 template code program
10 template code program
 
Python programing
Python programingPython programing
Python programing
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Python
PythonPython
Python
 
Programming egs
Programming egs Programming egs
Programming egs
 
Pseudocode
PseudocodePseudocode
Pseudocode
 

Pseudo code practice problems+ c basics

  • 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.
  • 6. Homework1 solution1: PseudoCode: Readnum1, num2,num3 If (num1 < num2) If(num2< num3) Write num1 ,num2, num3 Else If(num3< num1) Write num3,num1, num2 Else Write num1,num3, num2 else If(num1< num3) Write num2 ,num1, num3 Else If(num3< num2) Write num3,num2, num1 Else Write num2,num3, num1 CH code: intnum1, num2,num3; cin>> num1>>num2>>num3; if (num1 < num2) { if(num2< num3) { Cout<< num1<<" "<<num2<<" "<<num3; } else { if(num3< num1) { Cout<< num3<<" "<<num2<<" "<<num2; } else { Cout<< num1<<" "<<num3<<" "<<num2; } } } else { If(num1< num3) { Cout<< num2<<" "<<num1<<" "<<num3; } else { If(num3< num2) { Cout<< num3<<" "<<num2<<" "<<num1; } else { Cout<< num2<<" "<<num3<<" "<<num1; } } } Homework1 solution2: PseudoCode: Readnum1, num2,num3 If (num1 < num2 < num3) Write num1 , num2, num3 else If (num1< num3 < num2) Write num1 , num2, num3 else If (num2< num1 < num3) Write num2 , num1, num3 else If (num2< num3 < num1) Write num2 , num3, num1 else If (num3< num1 < num2) Write num3 , num1, num2 else If (num3< num2< num1) Write num3 , num2, num1 CH code: intnum1, num2; cin>> num1>>num2>>num3; if(num1< num2 && num2 < num3&& num1 < num3) { cout<<num1<<" "<<num2<<" "<<num3; } else if(num1< num2 && num2 > num3&& num1 < num3) { cout<<num1<<" "<<num3<<" "<<num2; } else if(num1> num2 && num2 < num3&& num1 < num3) { cout<<num2<<" "<<num1<<" "<<num3; } else if(num1> num2 && num2 < num3&& num1 > num3) { cout<<num2<<" "<<num3<<" "<<num1; } else if(num1< num2 && num2 > num3&& num1 > num3) { cout<<num3<<" "<<num1<<" "<<num2; } else if(num1> num2 && num2 > num3&& num1 > num3) { cout<<num3<<" "<<num2<<" "<<num1; }
  • 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".