Top Banner
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 Page 1 of 21
21

Secret Behind C++ Programming By Musa Mustapha

Feb 20, 2023

Download

Documents

MUSTAPHA MUSA
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Secret Behind C++ Programming By Musa Mustapha

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 hereas well string UNCAware = "\\\\?\\"; UNCAware += filePattern; findFirstHandle= ::FindFirstFileW(UNCAware.c_str(), findData); #else string UNCAware = filePattern; findFirstHandle= ::FindFirstFileA(UNCAware.c_str(), findData); #endif

Practical approach Page 1 of 21

Page 2: Secret Behind C++ Programming By Musa Mustapha

Solved exercises and assignments andtest questions

BY Musa Mustapha

To get A++ is EasyIf conditional statement syntax sample

#include <iostream> //header file for cinand cout

#include <string> // header file forcharacter

#include <conio> //header file forgetch , to retain the result on screen

int main ( ) // It tellswhere the program begins

{ // opening braces

char answer; // declaration

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

cin>> answer; // input from user

if (answer==’yes’) // conditionalstatement

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

Page 2 of 21

Page 3: Secret Behind C++ Programming By Musa Mustapha

else // if the 1st condition fails then it will executethis statement

cout<<”keep it aside”<<endl;

getch ( ); // toretain the result

} // closingbraces

Caution!Whenever you see double slash like this // means acomment or an explanatory statement for you tounderstand, it does not have an effect of theexecution of the program

Learn a programming in 1 hour a day

PrefatoryI purposefully compose this solutions to our dearcolleague’s problem from other department, whopetrified of C++ course, going by the last test mostof us already forecast our result in programing andfound likely to be below average. This minuscule pageswill give you an understanding, a good grade inprogramming course. I surprise why our colleagues from

Page 3 of 21

Page 4: Secret Behind C++ Programming By Musa Mustapha

other department afraid of programming, it is toosimple and very elementary at this stage, may be thetricky is most of us dearth the practical aspect orterrified of computer operating.Moreover, most of us find this course as invaluable toour studies especially EMT and Mathematics students. Ilearnt that you misunderstood the course as a resultof deficiency of familiarity in application ofprogramming into your major. Nowadays, computer hasbecome part of human being activities, noorganisation, academic or any field can do withoutcomputer, therefore possession of computer knowledgecan helps us in one way or the other in future time. However, it wasn’t too late for you to earn a verygood grade in Jungle++, at least you have anoutstanding time toward examination for you to earn agood grade at least a “A” or “B+” in programmingproblem. C++ does not required a much computational experiencenor mastering an equations, it only need understandingof the concept behind the scene. So now it’s time toblast the secret behind the scene, that’s why, Iintent to summarised the basic area covered from thecourseware and solve an assignments, lab exercise andtest questions, so that it will support us in ourstudy toward imminent examination by the grace ofAllah.Please do not depend on this material purposely for anexamination, this passage is based on practicalapproach, you must read the theoretical aspect for youto understand this clearly. And for those who are

Page 4 of 21

Page 5: Secret Behind C++ Programming By Musa Mustapha

interested in more practical aspect, we may likelyhold general revision class on programming, whichscheduled as follows, firstly, practical aspect andsecondly, resolve indistinct theory then answer someskeptical question relevant to an examination, on aday before C++ examination.Please do test run the programs in your computer toensure it credibility, because I drafted them offhead, due to time factor I cannot able to summarizethe whole courseware, I will only solve the exercisesand test questions. Lastly, I am not blameable for any offence you bumpinto by wronging with this texts either by jotting,recording, snapping, tearing or whatever mean oftransmission you use to constrain malpractice.

Week 1 exerciseLab 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 statementint main ( ){int row , column;

for (row=1; row<=12; row++) { for (column=1;column<=12;column++)

{cout<<setw(5)<<row*column;

Page 5 of 21

Page 6: Secret Behind C++ Programming By Musa Mustapha

}cout<<endl;

}getch ( );}

This will be the output of the above program.

Week 2 exerciseLab Questions

Q1. Write a program that will prints the following x` shape:

* ** *

* **

* ** *

* *Solution

Page 6 of 21

Page 7: Secret Behind C++ Programming By Musa Mustapha

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

{cout<<” * *“<<endl;cout<<” * * “<<endl;cout<<” * * “<<endl;cout<<” * “<<endl;cout<<” * * “<<endl;cout<<” * * “<<endl;cout<<” * *“<<endl;

getch ();}

Q2. Write a program to compute and output the perimeter of andarea 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 integernumbers, subtract the first integer from the second integerand display the result on the screen.

Page 7 of 21

Page 8: Secret Behind C++ Programming By Musa Mustapha

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);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 ( );

Page 8 of 21

Page 9: Secret Behind C++ Programming By Musa Mustapha

}

Week 3Lab Questions

1.Write a program that store the detail of your bankaccount. Declare Name of the bank, Account Name, AccountType, Account Number and Current Balance as yourvariables, 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’;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>

Page 9 of 21

Page 10: Secret Behind C++ Programming By Musa Mustapha

int main ( ){int bank_name, account_name, account_type, account_number,current_balance;cout<<” enter your bank’s 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 Questions1.Write a program(s) to I identify between the differences

between pre- incrementation and post incrementation

#include < iostream>#include < conio>//program for pre-incrementation;int main ( ){

int x, y;x =3;y=++x;cout<<”the value of y is <<y<<endl;

getch () ;Page 10 of 21

Page 11: Secret Behind C++ Programming By Musa Mustapha

}ExplanationThe 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 () ;}ExplanationThe new value of x will be 3 , and y too will be 4;

Week 5Lab 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>

Page 11 of 21

Page 12: Secret Behind C++ Programming By Musa Mustapha

#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;}

if(n>180)cout<<" square is not allowed for numbers over

180"<<endl;getch ( );} Q2. Improve program above with else…if 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;}

elsecout<<” square is not allowed for numbers over

180”<<endl;Page 12 of 21

Page 13: Secret Behind C++ Programming By Musa Mustapha

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;

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 herfirst name, and then stores it in a character array. Theprogram checks the first character of the array to seewhether it falls in the first half of the alphabet. If itdoes, an appropriate message is displayed.

#include <iostream>#include <string>#include <conio>Int main ( ){Char name [25];int n;cout<<”Type your name”<<endl;

Page 13 of 21

Page 14: Secret Behind C++ Programming By Musa Mustapha

cin>>n;

is an assignment for you to complete this program

Last ExerciseLab 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 ( ){ int z=10;do

{ cout<<z++<<; cout<<” “<<endl;}

while ( z<=20);getch ();}

Page 14 of 21

Page 15: Secret Behind C++ Programming By Musa Mustapha

Q2. Write a weather-calculator program that asks for a list ofthe previous 10 days’ temperatures, computes the average, andprints the results. You have to compute the total as the inputoccurs, then divide that total by 10 to find the average. Usea 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<<" daysis"<<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 ( ){ int day=0; float average, temp, sum=0;do {

Page 15 of 21

Page 16: Secret Behind C++ Programming By Musa Mustapha

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 inExercise 2, but generalize it so it computes the average ofany number of days’ temperatures. (Hint: You have to count thenumber 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;

Page 16 of 21

Page 17: Secret Behind C++ Programming By Musa Mustapha

cout<<" the total temperature for" <<day<<"days is"<<sum<<endl;getch ( );}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;

Page 17 of 21

Page 18: Secret Behind C++ Programming By Musa Mustapha

getch ( );}

Q8. Write a program that asks the user for her or his age. Usea for loop to print “Happy Birthday!” for every year of theuser’s age.#include <iostream>#include <conio>

int main ( ){ int answer;

cout<<"enter the your age "; cin>>answer;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 errors3.A function name must be followed by opening and closing braces4.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

Page 18 of 21

Page 19: Secret Behind C++ Programming By Musa Mustapha

Compare two operands8.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 111. 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;elsecout<<”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 endl2..b is used without declaration3.( ) is omitted in the int main

Q&A for Test 2 Instruction: Answer one question only.

Q 1 a. why main ( ) function is required in C++ program?

It tells where the program begins. The identifier main is thename of a function. Every C++ program must have one and onlyone main function. The required parentheses that follow theword “main” indicate that it is a function.

Page 19 of 21

Page 20: Secret Behind C++ Programming By Musa Mustapha

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 longas test expression is True (nonzero), the block of one or moreC++ statements executes repeatedly until test expressionbecomes False (evaluates to zero).

do.. while: The do…while statement controls the do…while loop,This ensures the body of the loop executes at least once. Thedo-while tests for a positive relational test; as long as the test isTrue, the body of the loop continues to execute. The syntaxfor the do…while statement is :

Page 20 of 21

Page 21: Secret Behind C++ Programming By Musa Mustapha

do { block of one or more C++ statements; } while (condition)

Where condition is an integral expression and statement is anyexecutable statement. It repeatedly executes the statement andthen evaluates the condition until that condition evaluates tofalse.2. b. Write a weather-calculator program that asks for a

list of the previous 10 days’ temperatures, computes theaverage, and prints the results. You have to compute thetotal as the input occurs, then divide that total by 10 tofind 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<<" daysis"<<average<<endl;cout<<" the total temperature for "<<day<<"days is"<<sum<<endl;getch ( );}

Page 21 of 21