Você está na página 1de 17

SECRET BEHIND C+

+
Never afraid for such codes . again
LPWIN32_FIND_DATA FindFirstHelper::ResetFindData(){
if(findData == NULL)
{findData = new WIN32_FIND_DATA;}
else{::FindClose(findFirstHandle);
}
return findData;
}
bool
FindFirstHelper::FindFirst(const
char
*filePattern){
ResetFindData();
#if 0
// gmc revist: we should handle UNC filenames here
as well
string UNCAware = "\\\\?\\";
UNCAware += filePattern;
findFirstHandle
=
::FindFirstFileW(UNCAware.c_str(), findData);
#else
string UNCAware = filePattern;
findFirstHandle
=
::FindFirstFileA(UNCAware.c_str(), findData);
#endif

Practical approach
Solved exercises and assignments and test
questions
Page 1 of 17

BY Musa Mustapha

To get A++ is Easy


If conditional statement syntax sample
#include <iostream>

//header file for cin and cout

#include <string>

// header file for character

#include <conio>
the result on screen

//header file for getch , to retain

int main ( )

// It tells where the program begins

// opening braces
char answer;

// declaration

cout<<do you have problem with c++<<endl;


cin>> answer;

// input from user

if (answer==yes)

// conditional statement

cout<<this is the right solution to you<<endl; // it will


display this
else
// if the 1st condition fails then it will
execute this statement
cout<<keep it aside<<endl;
getch ( );
}

// to retain the result


// closing braces

Caution!
Whenever you see double slash like this // means a comment or
an explanatory statement for you to understand, it does not
have an effect of the execution of the program

Page 2 of 17

Learn a programming in 1 hour a day

Prefatory
I purposefully compose this solutions to our dear colleagues
problem from other department, who petrified of C++ course,
going by the last test most of us already forecast our result in
programing and found likely to be below average. This
minuscule pages will give you an understanding, a good grade in
programming course. I surprise why our colleagues from other
department afraid of programming, it is too simple and very
elementary at this stage, may be the tricky is most of us dearth
the practical aspect or terrified of computer operating.
Moreover, most of us find this course as invaluable to our
studies especially EMT and Mathematics students. I learnt that
you misunderstood the course as a result of deficiency of
familiarity in application of programming into your major.
Nowadays, computer has become part of human being
activities, no organisation, academic or any field can do without
computer, therefore possession of computer knowledge can
helps us in one way or the other in future time.
However, it wasnt too late for you to earn a very good grade in
Jungle++, at least you have an outstanding time toward
examination for you to earn a good grade at least a A or B+
in programming problem.
C++ does not required a much computational experience nor
mastering an equations, it only need understanding of the
concept behind the scene. So now its time to blast the secret
behind the scene, thats why, I intent to summarised the basic
area covered from the courseware and solve an assignments,
lab exercise and test questions, so that it will support us in our
study toward imminent examination by the grace of Allah.
Page 3 of 17

Please do not depend on this material purposely for an


examination, this passage is based on practical approach, you
must read the theoretical aspect for you to understand this
clearly. And for those who are interested in more practical
aspect, we may likely hold general revision class on
programming, which scheduled as follows, firstly, practical
aspect and secondly, resolve indistinct theory then answer
some skeptical question relevant to an examination, on a day
before C++ examination.
Please do test run the programs in your computer to ensure it
credibility, because I drafted them off head, due to time factor I
cannot able to summarize the whole courseware, I will only solve
the exercises and test questions.
Lastly, I am not blameable for any offence you bump into by
wronging with this texts either by jotting, recording, snapping,
tearing or whatever mean of transmission you use to constrain
malpractice.

Week 1 exercise
Lab Questions

Q1. Identify any real world problem and provide a suitable solution using
your knowledge of problem solving methodology.
Solution to the Problem of drafting multiplication table
#include <iostream>
#include <iomanip>
#include <conio>
//program to generate multiplication table using for loop nested
statement
int main ( )
{
int row , column;
for (row=1; row<=12; row++)
{
for (column=1;column<=12;column++)
{
cout<<setw(5)<<row*column;
}
cout<<endl;
}

Page 4 of 17

getch ( );
}
This will be the output of the above program.

Week 2 exercise
Lab Questions
Q1. Write a program that will prints the following x` shape:
*

*
*

**
*
* *
*
*
*
*
Solution
#include <iostream>
#include <conio>
//program to display the shape of x;
int main ( )
{
cout<<
*
*
cout<<
*
*
cout<<
*
*

<<endl;
<<endl;
<<endl;

Page 5 of 17

cout<<
cout<<
cout<<
cout<<
getch ();
}

*
*
*
*

*
*

<<endl;
<<endl;
<<endl;
* <<endl;

Q2. Write a program to compute and output the perimeter of and area of
a circle having a radius of 3m
Solution
#include <iostream>
#include <conio>
//program to computer area and perimeter of a circle;
int main ( )
{
float area, perimeter;
int radius;
radius = 3;
area = 2*3.142*radius*radius;
perimeter = 3.142*radius*radius;
cout<< the area of a circle having 3 meter radius is
<<area<<endl;
cout<< the perimeter of a cirle having 3 meter radius is
<<perimeter<<endl;
getch ( );
}
Q3. Write a program that will allow inputting two integer numbers,
subtract the first integer from the second integer and display the result
on the screen.
Assume your variables are x and y;
solution
#include <iostream>
#include <conio>
int main ( )
{
int x, y;
float result;
cout<< enter a the value of y <<endl;
cin>>y;
cout<<enter a the value of x<<endl;
cin>>x;
result = (x y);

Page 6 of 17

cout<< the result of x subtract from y is<< result <<endl;


getch ();
}
Q4. Write the C++ code to store three variables: your weight, height in
feet, and shoe size. Declare the variables, assign their values in the body
of your program and then output the values.
#include <iostream>
#include <string>
#include <conio>
int main ( )
{
char height, shoe_size , weight ;
height = 6 feet;
shoe_size = 43 cm;
weight = 120kg;
cout<< your height is <<height<<endl;
cout<<your weight is <<weight<<endl;
cout<< and your shoe size is <<shoe_size<<endl;
getch ( );
}

Week 3
Lab Questions

1. Write a program that store the detail of your bank account. Declare
Name of the bank, Account Name, Account Type, Account Number
and Current Balance as your variables, assign values and display
them on the screen.
#include <iostream>
#include <string>
#include <conio>
int main ( )
{
char bank_name, account_name, account_type, account_number,
current_balance;
bank_name = first bank plc;

Page 7 of 17

account_name = bjarne stroustrop;


account_type = current;
account_number = 3984747;
current_balance = 974,765,365,353;
cout<<bank name<<bank_name<<endl;
cout<<account name<<account_name<<endl;
cout<<account type<<account_type<<endl;
cout<<account number<<account_number<<endl;
cout<<account balance<<account_balance<<endl;
getch ( );
}
2. Modify the program in 1.) Above to receive keyboard input.
#include <iostream>
#include <conio>
int main ( )
{
int bank_name, account_name, account_type, account_number,
current_balance;
cout<< enter your banks name<<endl;
cin>>bank_name;
cout<< enter account your account name<<endl;
cin>>account_name;
cout<< enter your accout type<<endl;
cin>>account_type;
cout<<enter your account numbet<<endl;
cin>>account_number;
cout<< enter your current balance<<endl;
cin>>current_balance ;
cout<< Your Details Are <<endl;
cout<<Bank Name<<bank_name<<endl;
cout<<Account Name<<account_name<<endl;
cout<<Account Type<<account_type<<endl;
cout<<Account Number<<account_number<<endl;
cout<<Account Balance<<account_balance<<endl;
getch ( );
}

Week 4 exercise
Lab Questions
1. Write a program(s) to I identify between the differences between
pre- incrementation and post incrementation
#include < iostream>
#include < conio>

Page 8 of 17

//program for pre-incrementation;


int main ( )
{
int x, y;
x =3;
y=++x;
cout<<the value of y is <<y<<endl;
getch () ;
}
Explanation
The new value of x will be 4, and y too will be 4;
#include < iostream>
#include < conio>
//program for post-incrementation;
int main ( )
{
int x, y;
x =3;
y=x++;
cout<<the value of y is <<y<<endl;
getch () ;
}
Explanation
The new value of x will be 3 , and y too will be 4;

Week 5

Lab Questions
Q1. Write a program that will takes value from the user and print its
square and cube unless it is more than 180. The message ***square
is not allowed for numbers over 180*** appear on - screen.
#include <iostream>
#include <conio>
int main ( )
{

Page 9 of 17

int n, square, cube;


cout<<" enter a positive number "<<endl;
cin>>n;
if(n<=180)
{
square=n*n;
cube = n*n*n;
cout<<" the square of "<<n<<"is "<<square<<endl;
cout<<" and the cube" <<n<<"is"<<cube<<endl;
}
if(n>180)
cout<<" square is not allowed for numbers over 180"<<endl;
getch ( );
}
Q2. Improve program above with elseif statement.
#include <iostream>
#include <conio>
int main ( )
{
int n, square, cube;
cout<< enter a positive number <<endl;
cin>>n;
if(n<=180)
{
square=n*n;
cube = n*n*n;
cout<< the square of <<n<<is <<square<<endl;
cout<< and the cube <<n<<is<<cube<<endl;
}
else
cout<< square is not allowed for numbers over 180<<endl;
getch ( );
}
Q3. Write a program that will asks user for a number. It then prints
whether the number is greater then zero or less then or equal to
zero.
#include <iostream>
#include <conio>
int main ( )
{
int n;
cout<< enter a number <<endl;
cin>>n;

Page 10 of 17

if(n>0)
cout<< the number is positive<<endl;
if(n<=0)
cout<< the number is less than or equal to zero<<endl;
getch ( );
}
Q4. Write program that will ask the user for his or her first name,
and then stores it in a character array. The program checks the first
character of the array to see whether it falls in the first half of the
alphabet. If it does, an appropriate message is displayed.
#include <iostream>
#include <string>
#include <conio>
Int main ( )
{
Char name [25];
int n;
cout<<Type your name<<endl;
cin>>n;

is an assignment for you to complete this program

Last Exercise
Lab Questions
Q1. Write a program with a do-while loop that prints the numerals from
10 to 20 (inclusive), with a blank line between each number.
Solution
#include <iostream>
#include <conio>
int main ( )

Page 11 of 17

{
int z=10;
do
{
cout<<z++<<;
cout<< <<endl;
}
while ( z<=20);
getch ();
}
Q2. Write a weather-calculator program that asks for a list of the
previous 10 days temperatures, computes the average, and prints the
results. You have to compute the total as the input occurs, then divide
that total by 10 to find the average. Use a while loop for the 10
repetitions.
Solution
#include <iostream>
#include<conio>
int main ( )
{
int day=0;
float average, temp, sum=0;
while (day!=10)
{
cout<<" enter the temperature for day"<< (day+1)<<" : ";
cin>>temp;
sum+=temp;
day++;
}
average= (sum)/day;
cout<<" the average temperature for "<<day<<" days
is"<<average<<endl;
cout<<"
the
total
temperature
for
"<<day<<"days
is
"<<sum<<endl;
getch ( );
}
Q3. Rewrite the program in Exercise 2 using a do-while loop.
#include <iostream>
#include<conio>
int main ( )
{

Page 12 of 17

int day=0;
float average, temp, sum=0;
do
{
cout<<" enter the temperature for day"<<(day+1)<<endl;
cin>>temp;
sum+=temp;
day++;
}
while (day!=10);
average= (sum)/day;
cout<<"
the
average
temperature
for
"<<day<<"days
is"<<average<<endl;
cout<<"
the
total
temperature
for
"<<day<<"days
is
"<<sum<<endl;
getch ( );
}
Q4. Write a program, similar to the weather calculator in Exercise 2, but
generalize it so it computes the average of any number of days
temperatures. (Hint: You have to count the number of temperatures to
compute the final average.)
#include <iostream>
#include<conio>
int main ( )
{
int day=0;
float average, temp, sum=0;
while (day!=10)
{
cout<<" enter the temperature for day"<<(day+1)<<endl;
cin>>temp;
sum+=temp;
day++;
cout<<" the average temperature from day 1 to day "<<day<<"is
"<<(sum)/day<<endl;
}
average= (sum)/day;
cout<<" the average temperature for "<<day<<" days
is"<<average<<endl;
cout<<"
the
total
temperature
for"
<<day<<"days
is
"<<sum<<endl;
getch ( );
}

Page 13 of 17

Q5. Write a program that prints the numerals 1 to 15 on-screen. Use a


for loop to control the printing.
#include <iostream>
#include <conio>
int main ( )
{
int p;
for (p=1; p<=15; p++)
cout<<p<<endl;
getch ( );
}
Q6. Write a program to print the numerals 15 to 1 on-screen. Use a for
loop to control the printing.
#include <iostream>
#include <conio>
int main ( )
{
int j;
for (j=15; j ! =0; j- -)
cout<<j<<endl;
getch ( );
}
Q7. Write a program that uses a for loop to print every odd number from
1 to 100.
#include <iostream>
#include <conio>
int main ( )
{
int w;
for (w=1; w<=100; w+=2)
cout<<w<<endl;
getch ( );
}
Q8. Write a program that asks the user for her or his age. Use a for loop
to print Happy Birthday! for every year of the users age.
#include <iostream>
#include <conio>
int main ( )
{
int answer;
cout<<"enter the your age ";
cin>>answer;

Page 14 of 17

for (int p=1; p<=answer; p++)


cout<<"Happy Birthday @ year"<<p<<endl;
getch ( );
}

Q&A for Test 1


Instruction: attempt all.
1. << is the operator that writes output to the screen.
2. What does the term debug mean? Act of identifying and
correcting errors
3. A function name must be followed by opening and closing braces
4. What kind of program elements are the following?
a. 12 interger b. a character
c. junglejim () function d.
junglejim variable
5. Write a statement that gets a numerical value from the keyboard
and places it in the variable temp.
int temp, a;
cin>>a;
temp=a;
cout<<temp<<endl;
6. The increment operator increases the value of a variable by how
much 1
7. A relational operator
Compare two operands
8. Write an expression that uses a relational operator to return if the
variable Fatima is not equal to Nana.
If ( Fatima != Nana)
Cout<<true<<endl;
9. A function body is delimited by a curly braces
10.
The expression 13%3 evaluated to 4 reminder 1
11.
Write an if.else statement that displays YES if a variable age
is greater than 21, and display NO otherwise.
if(age>21)
cout<<Yes<<endl;
else
cout<<No<<endl;
12.
Identify error in the following portion of program
Int main
{
Int a=b+5;
cout<<a is <<a<<endl
}
1. No semi colon ; at the endl
2. .b is used without declaration

Page 15 of 17

3. ( ) is omitted in the int main

Q&A for
Q 1 a.

Test 2

Instruction: Answer one question only.


why main ( ) function is required in C++ program?

It tells where the program begins. The identifier main is the name of a
function. Every C++ program must have one and only one main function.
The required parentheses that follow the word main indicate that it is a
function.
b. write a program that will receive an integer number n and return the
factorial of that number to your program. The program should check if n
is 0 then it prints 1.
#include <iostream>
#include <conio>
int main()
{
int n, factor=1;
cout << "Enter an interger number: "; cin >> n;
if(n==0)
cout<<"1"<<endl;
else;
for (int j=n; j>0; j- -)
factor * = j;
cout << "Factorial is " << factor << endl;
getch ();
}
Q 2. a.

state the difference between while and do..while loop.

While: The while, is a looping statement that controls the execution of a


series of other statements. Looping statements cause parts of a program
to execute repeatedly, as long as a certain condition is being met. Below
is the format of while.
while (test expression)
{block of one or more C++ statements; }
The parentheses around test expression are required. As long as test
expression is True (nonzero), the block of one or more C++ statements

Page 16 of 17

executes repeatedly until test expression becomes False (evaluates to


zero).
do.. while: The dowhile statement controls the dowhile loop, This
ensures the body of the loop executes at least once. The do-while tests
for a positive relational test; as long as the test is True, the body of the
loop continues to execute. The syntax for the dowhile statement is :
do
{ block of one or more C++ statements; }
while (condition)
Where condition is an integral expression and statement is any
executable statement. It repeatedly executes the statement and then
evaluates the condition until that condition evaluates to false.
2.
b. Write a weather-calculator program that asks for a list of the
previous 10 days temperatures, computes the average, and prints
the results. You have to compute the total as the input occurs, then
divide that total by 10 to find the average. Use a while loop for the
10 repetitions.
solution
#include <iostream>
#include<conio>
int main ( )
{
int day=0;
float average, temp, sum=0;
while (day!=10)
{
cout<<" enter the temperature for day"<< (day+1)<<" : ";
cin>>temp;
sum+=temp;
day++;
}
average= (sum)/day;
cout<<"
the
average
temperature
for
"<<day<<"
days
is"<<average<<endl;
cout<<" the total temperature for "<<day<<"days is "<<sum<<endl;
getch ( );
}

Page 17 of 17

Você também pode gostar