c++ Solutions eedsohag.epizy.com Ahmed Ali

Size: px
Start display at page:

Download "c++ Solutions eedsohag.epizy.com Ahmed Ali"

Transcription

1 c++ s Ahmed Ali int main(int argc, char *argv[]) int age; age=10; cout<<" You are "<<age<<" years old.\n"; cout<<" You are too young to play the game.\n"; system("pause"); //2 int main(int argc, char *argv[]) cout<<"*****\n"; cout<<"*****\n"; cout<<"*****\n"; cout<<"*****\n"; cout<<"*****\n"; system("pause"); //3 Exercise 3: Write a C++ program to declare two integer, one float variables and assign 10, 15, and 12.6 to them respectively. It then prints these values on the screen. : int main(int argc, char *argv[]) int x; int y; float z; x=10; y=15; z=12.6; cout<<"x="<<x<<"\t"<<"y="<<y<<"\t"<<"z="<<z; cout<<"\n"; system("pause"); //4 Exercise 4: Write a C++ program to prompt the user to input her/his name and print this name on the screen, as shown below. The text from keyboard can be read by using cin>> and to display the text on the screen you can use cout<<. Hello Sok!.

2 c++ s Ahmed Ali : int main(int argc, char *argv[]) char name[20]; cout<<"enter your name:"; cin>>name; cout<<"hello "<<name<<"! \n"; system("pause"); //5 Exercise 5: Write a C++ program to prompt the user to input 3 integer values and print these values in forward and reversed order, as shown below. Please enter your 3 numbers: Your numbers forward: Your numbers reversed: : int main(int argc, char *argv[]) int val1; int val2; int val3; cout<<"please enter your 3 numbers:"; cin>>val1>>val2>>val3; cout<<"\nyour numbers forward:\n"; cout<<val1<<"\n"<<val2<<"\n"<<val3<<"\n"; cout<<"your numbers reversed:\n"; cout<<val3<<"\n"<<val2<<"\n"<<val1<<"\n"; system("pause"); //sheet 2

3 c++ s Ahmed Ali EXERCISE 1 Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between 56 and 78 (both included). In the other case it writes "YOU LOSE". int a; cout << "Type an integer : "; cin >> a; if ( (a >= 56) && (a <= 78) ) cout << "YOU WIN" << endl; else cout << "YOU LOSE" << endl; EXERCISE 2 Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. A solution that uses input from users could be int a = 8; int number; cout << "Enter all the numbers from 8-23.\n"; for(; a < 24;) cin >> number; if(number == a) a++; cout << "Next number:\n"; else cout << "?"; int typedint = 0; cout << "Type all numbers between 8 and 23: " << endl; for(int i = 8; i <= 23; i++)

4 c++ s Ahmed Ali cin >> typedint; if (typedint!= i) cout << "You Missed the sequence the next number was " << i << endl; EXERCISE 3 Same exercise but you must use a while. A solution that uses input from the user could be int a = 8, number; cout << "Enter numbers from 8 to 23\n"; while(a < 24) cin >> number; if(a == number) cout << "Next number: "; a++; else cout << "?"; EXERCISE 4 Write a program that asks the user to type 10 integers and writes the sum of these integers. /* A program to sum 10 integers using a loop*/ //Define variables int i = 1; int n = 0; int temp; //Counter //Sum //Input store cout << "This program sums ten user entered integers\n\n"; for (i = 1 ; i <= 10 ; i++) cout << "Type integer " << i << ": "; cin >> temp;

5 c++ s Ahmed Ali n = n + temp; cout << "\nthe sum of the integers is: " << n << endl; int i,s=0,x; for(i=0;i<10;i++) cout<<"type an integer: ";cin>>x; s=s+x; cout<<"the sum is : "<<s<<endl; EXERCISE 5 Write a program that asks the user to type 10 integers and writes the smallest value. int value = 0, min = 0; cout<<"enter 10 values and I will tell you which one is the smallest"<<endl; cout<<"enter number: "<<endl; cin >> value; min = value; for(int i =0; i < 9; i++) cout<<"enter number: "<<endl; cin>>value; if(value < min) min = value; cout<<"smallest value is: "<<min<<endl;

6 c++ s Ahmed Ali EXERCISE 6 Write a program that asks the user to type an integer N and computes the sum of the cubes from 53 to N3. SOLUTION: //Calling the appropriate libraries //Declare variables. Iniatial value for p is 125 (53) int i, n, p=125; //Ask the user to enter a number then save to variable n cout<<"enter a number: \n"; cin>>n; //This will compute the sum of the cubes if the value of variable n is greater than 5 for(i=n;i>5;i--)p+=i*i*i; //This will compute the sum of the cubes if the value of variable n is less than 5 for(i=n;i<5;i++)p+=i*i*i; //Displays the sum of the cube cout<<"the sum of cube of 5 up to cube of "<<n<<" is "<<p; system("pause"); EXERCISE 7 Write a program that asks the user to type an integer N and compute u(n) defined with : u(0)=3 u(n+1)=3*u(n)+4 : int n; int func = 0;

7 c++ s Ahmed Ali cout<<"enter a value: "<<endl; cin>>n; for(int i = 0; i < n; i++) if(i == 0) func += 3; func = 3*func +4; cout<<"the result of the computed function is: "<< func <<endl; EXERCISE 8 Write a program that asks the user to type an integer N and compute u(n) defined with : u(0)=1 u(1)=1 u(n+1)=u(n)+u(n-1) #include <cmath> int u(int n) if (n<=1) return n; else return u(n-1)+u(n-2); int a; cout<<"enter an integer: "; cin>>a; cout<<"u("<<a<<")="<<u(a); EXERCISE 9 Write a program that asks the user to type an integer between 0 and 20 (both included) and writes N+17. If someone types a wrong value, the program writes ERROR and he must type another value. int N; bool ok; do cout<<"type the value of N between 0 et 20 :";cin>>n; ok= N<=20 && N>=0; if(!ok)cout<<"error"<<endl;

8 c++ s Ahmed Ali while(!ok); N=N+17; cout<<"the final value is : "<<N<<endl; EXERCISE 10 Write a program that is able to compute some operations on an integer. The program writes the value of the integer and writes the following menu : 1. Add 1 2. Multiply by 2 3. Subtract 4 4. Quit The programs ask the user to type a value between 1 and 4. If the user types a value from 1 to 3 the operation is computed, the integer is written and the menu is displayed again. If the user types 4, the program quits. int n, op; bool quit = false; while(!quit && (cin >> n) ) //get input if 'quit' is false cout << "What do you want to do with " << n << '?' << endl << "1. Add 1" << endl << "2. Multiply by 2" << endl << "3. Subtract 4" << endl << "4. Quit" << endl; cin >> op; switch(op) case 1: cout << n + 1 << endl; break; case 2: cout << n * 2 << endl; break; case 3: cout << n - 4 << endl; break; case 4: quit = true; break; default: cout << "Unknown operation!" << endl; break;

9 c++ s Ahmed Ali EXERCISE 11 Write a program that asks the user to type a positive integer. When the user types a negative value the program writes ERROR and asks for another value. When the user types 0, that means that the last value has been typed and the program must write the average of the positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'. int x, sum = 0,max = 0; double average; do cout<<"type an integer:"; cin>>x; if (x > 0 ) sum += x; max++; else if(x<0) cout << "ERROR "; while(x!=0); if(max == 0) cout << "NO AVERAGE"<<endl; else average = sum / max; cout<<"the average is : "<<average<<endl; system("pause"); EXERCISE 12 Write a program that asks the user to type an integer N and compute u(n) defined with :

10 c++ s Ahmed Ali u(0)=3 u(1)=2 u(n)=n*u(n-1)+(n+1)*u(n-2)+n int f(int x) if(x==0 x==1) if(x == 0) return 3; if(x == 1) return 2; else return x*f(x-1)+(x+1)*f(x-2)+x; int n; cout << "Enter an integer: " << endl; cin >> n; cout << endl << "Result: " << f(n); EXERCISE 13 Write a program that asks the user to type 10 integers and write the number of occurrence of the biggest value. void main() int number, biggest(0), occur(0); for (int i = 1; i < 11 ; i++) cout << "Input " << i << ": "; cin >>number; if (number > biggest) biggest = number; occur = 0; if (biggest == number) occur++; cout << "Biggest number inputted was: " << biggest << " \noccurance: " << occur << endl; system("pause"); EXERCISE 14 Write a program that asks the user to type the value of N and computes N!.

11 c++ s Ahmed Ali int num; cout << "Enter a number: "; cin >> num; long fact = 1; for (int i = 1; i <= num; i++) fact *= i; cout <<"!"<< num<< "= "<< fact; EXERCISE 15 Write a program that asks the user to type an integer N and that indicates if N is a number or not. void main() int num, i; bool prime = true; cout << "Enter a number: "; cin >> num; for (i = num-1; i > 1 && prime == true; i--) if (num%i == 0) prime = false; if (prime == true) cout << "Prime number"; else cout << "Not prime number"; system("pause"); EXERCISE 16 Write a program that asks the user to type an integer N and that writes the number of prime numbers lesser or equal to N. bool is_prime(int n)

12 c++ s Ahmed Ali if (n<2) return false; for (int i = 2; i < n; i++) if ((n % i) == 0) return false; return true; int temp = 0; cout << "Enter an integer: "; cin >> temp; for (int i = 2; i <= temp; i++) if (is_prime(i)) cout << i << endl; EXERCISE 17 Write a program that asks the user to type the value of an integer N and compute the N-st prime number. bool is_prime(int n) if (n<2) return false; for (int i = 2; i < n; i++) if ((n % i) == 0) return false;

13 c++ s Ahmed Ali return true; int i=0; int num = 0; int myval = 0; cout << "Enter an integer: "; cin>>num; if (num==0) cout<<"wrong degree"; int nst=0; while (nst<num) if (is_prime(i)) nst++; myval=i; i++; cout<<"the "<<num<<"-st prime number is "<<myval<<endl; EXERCISE 18 Write a program that asks the user to type the value of N and writes this picture : N=1 * N=2 ** * N=3 *** ** * int i,j,n; cout<<"type the value of N : "; cin>>n; for(i=1;i<=n;i++) for(j=1;j<=n+1-i;j++) cout<<"*"; cout<<endl;

14 c++ s Ahmed Ali EXERCISE 19 Write a program that asks the user to type the value of N and display an output like the following: N=1 * N=2 ** * N=3 *** ** * and so on... int i,j,n; cout<<"type the value of N : ";cin>>n; for(i=1; i<=n; i++) for (j=1; j<i; j++) cout<<" "; for (j=1; j<=n+1-i; j++) cout<<"*"; cout << endl; EXERCISE 20 u(n) is defined with: u(0) = a (a is an integer) if u(n) is even then u(n+1)=u(n)/2, else u(n+1)=3*u(n)+1 Conjecture: For all value of a, there exists a value N such that u(n)=1. a)write a program that asks the user to type the value of an integer a and writes all the values of u(n) from n=1 to n=n.

15 c++ s Ahmed Ali int a,n,u; cout<<"type the value of a : ";cin>>a; n=0; u=a; while(u!=1) if(u%2==0)u=u/2; else u=3*u+1; n++; cout<<"u("<<n<<")="<<u<<endl; b) Write a program that asks the user to type a value M and computes the value of a from 2 and M that maximizes the value of N. This value is called A. The program must write the value of A and N. int a,n,u,m,amax,nmax; cout<<"type the value of M : ";cin>>m; amax=2; nmax=2; for(a=3;a<=m;a++) n=0; u=a; while(u!=1) if(u%2==0)u=u/2; else u=3*u+1; n++; if(n>nmax)amax=a;nmax=n; cout<<"the value of A is :"<<amax<<endl; cout<<"the value of N is :"<<nmax<<endl; EXERCISE 21 Request the user to type numbers, each time printing its triple, until the user enters for (int input; input!= -999;) cout << "Enter a number (-999 to quit): ";

16 c++ s Ahmed Ali cin >> input; if (input!= -999) cout << "Tripled: " << input * 3 << endl; return(0); EXERCISE 22 Request the user to type positive numbers until either a zero or a negative is typed, and then show the user how many positives were typed in. int i,k=0,nr; for(i=1; i<=10000; i++) cin>>nr; if(nr>0) k=k+1; else break; cout<<k; EXERCISE 23 Request the user to type positive numbers, or to stop by typing a number smaller than 1. Print the average. int n, sum = 0, nums = 0; do cout << "Enter a number (less than 1 = quit): "; cin >> n; if( n > 0 ) sum += n; ++nums; while( n > 0 ); if( nums > 0 )

17 c++ s Ahmed Ali else cout << "The average is: " << sum / nums; cout << "No average"; cout << endl; EXERCISE 24 Request the user to type numbers, or type 0 to stop. Show how many numbers were between 100 and 200 (both included). int input, numberbetween(0); const short int minnum(100), maxnum(200); do cout << "Enter a number (0 to quit): "; cin >> input; if (input >= minnum && input <= maxnum) numberbetween++; while (input!= 0); cout << "Numbers between " << minnum << " and " << maxnum << ": " << numberbetween; return(0); EXERCISE 25 The country A has 50M inhabitants, and its population grows 3% per year. The country B, 70M and grows 2% per year. Tell in how many years A will surpass B. class City public: int inhabitants,gpy; //growth per year City(int a,int b) inhabitants=a; gpy=b; ; int main () City A(50,3),B(70,2); int i=0; begin: A.inhabitants+=A.gpy*A.inhabitants/100; B.inhabitants+=B.gpy*B.inhabitants/100; i++; if (A.inhabitants>B.inhabitants)

18 c++ s Ahmed Ali std::cout<<"in "<<i<<"year(s), A will surpass B\n"; else goto begin; EXERCISE 26 Write a program that asks the user to input 5 sequences of characters. Then it will ask the user for a character to search for and will output the maximum number of times that it occurred between the 5 sequences. Example: Sequence 1: aabc Sequence 2: aaaa If the user chooses to search for character 'a', the program will output "Character a occurred a maximum of 4 times" because it occurred 4 times in the second sequence, and only twice in the first sequence. #include <string> #include <vector> int a = 1; string stemp; vector<string> shold; cout << "Enter any sequence of characters. Enter up to 5 sequences.\n\n"; do cout << "Enter sequence " << a << ": "; cin >> stemp; shold.push_back(stemp); a++; while(a<6); char cseek; cout << "Enter a letter to look for and count its max occurrence: "; cin >> cseek; vector<int> vicount; for(int i=0; i<shold.size(); i++) //Counting the occurrences of cseek int icount = 0; for(int k=0; k<shold[i].size(); k++) if(shold[i][k] == cseek) icount++; vicount.push_back(icount); //The maximum occurrence of cseek for each sequence will be stored in vicount, each value referred to as a local maximum int imax = vicount[0]; for(int i=0; i<vicount.size(); i++) if(vicount[i] > imax) imax = vicount[i]; //Finding the max of the local maximums

19 c++ s Ahmed Ali cout << "The character " << cseek << " occurred a maximum of " << imax << " times.\n\n"; EXERCISE 27 Write a program that asks the user to type a random number.write in output if this number is a prime number or not. Write,by which numbers can your number be divided. int x; int count = -1; cout << "Write a number:\n"; cin >> x; getchar(); for(int i=1;i<x+1;i++) if(x%i == 0) count+=1; cout << "Number " << x << " can be divided by " << i << endl; if(count == 1) cout << "This number is prime number." << endl; else cout << "This number is not a prime number." << endl; getchar();

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition) Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.

More information

Control Structure and Loop Statements

Control Structure and Loop Statements Control Structure and Loop Statements A C/C++ program executes in sequential order that is the way the instructions are written. There are situations when we have to skip certain code in the program and

More information

Example 3. #include <iostream> using namespace std; int main()

Example 3. #include <iostream> using namespace std; int main() 1 Repetition Structure Examples Example 1 #include float number, sum=0; // number to be read cin >> number; // read first number cout

More information

VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS

VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS 1. Write C++ programs to interchange the values of two variables. a. Using with third variable int n1, n2, temp; cout

More information

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); } 1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to

More information

2. ARRAYS What is an Array? index number subscrip 2. Write array declarations for the following: 3. What is array initialization?

2. ARRAYS What is an Array? index number subscrip 2. Write array declarations for the following: 3. What is array initialization? 1 2. ARRAYS 1. What is an Array? Arrays are used to store a set of values of the same type under a single variable name. It is a collection of same type elements placed in contiguous memory locations.

More information

خ ث ح 13:10 14:00 خ ث ح 51:51 16:11 ر ن 09:41 11:11 ر ن

خ ث ح 13:10 14:00 خ ث ح 51:51 16:11 ر ن 09:41 11:11 ر ن Philadelphia University Faculty of Engineering Department of Computer Engineering Programming Language (630263) Date:- 07/02/2015 Allowed time:- 2 Hours Final Exam Student Name: -... ID: - Instructor:

More information

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs. 1 3. Functions 1. What are the merits and demerits of modular programming? Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

More information

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

More information

C++ PROGRAMMING SKILLS Part 2 Programming Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures C++ PROGRAMMING SKILLS Part 2 Programming Structures If structure While structure Do While structure Comments, Increment & Decrement operators For statement Break & Continue statements Switch structure

More information

1. Answer the following : a) What do you mean by Open Source Software. Give an example. (2)

1. Answer the following : a) What do you mean by Open Source Software. Give an example. (2) THE AIR FORCE SCHOOL Class XI First Terminal Examination 2017-18 Computer Science (083) Time: 3 hrs. M. Marks : 70 General Instructions : (i) All the questions are compulsory. (ii) Programming Language

More information

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays) CPSC 122 Study Guide: Examination 1 Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays) 1. What is the output? int x, y; x = y = 0; while(x < 5) y +=

More information

Looping statement While loop

Looping statement While loop Looping statement It is also called a Repetitive control structure. Sometimes we require a set of statements to be executed a number of times by changing the value of one or more variables each time to

More information

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met. Lecture 6 Other operators Some times a simple comparison is not enough to determine if our criteria has been met. For example: (and operation) If a person wants to login to bank account, the user name

More information

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver) Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program

More information

Local and Global Variables

Local and Global Variables Lecture 10 Local and Global Variables Nearly every programming language has a concept of local variable. As long as two functions mind their own data, as it were, they won t interfere with each other.

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ). LOOPS 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ). 2-Give the result of the following program: #include

More information

Functions. Introduction :

Functions. Introduction : Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

A Freshman C++ Programming Course

A Freshman C++ Programming Course A Freshman C++ Programming Course Dr. Ali H. Al-Saedi Al-Mustansiria University, Baghdad, Iraq January 2, 2018 1 Number Systems and Base Conversions Before studying any programming languages, students

More information

Study Guide for Test 2

Study Guide for Test 2 Study Guide for Test 2 Topics: decisions, loops, arrays, c-strings, linux Material Selected from: Chapters 4, 5, 6, 7, 10.1, 10.2, 10.3, 10.4 Examples 14 33 Assignments 4 8 Any syntax errors are unintentional

More information

C++ Programming Assignment 3

C++ Programming Assignment 3 C++ Programming Assignment 3 Author: Ruimin Zhao Module: EEE 102 Lecturer: Date: Fei.Xue April/19/2015 Contents Contents ii 1 Question 1 1 1.1 Specification.................................... 1 1.2 Analysis......................................

More information

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type. CS 7A - Fall 2016 - Midterm 1 10/20/16 Write responses to questions 1 and 2 on this paper or attach additional sheets, as necessary For all subsequent problems, use separate paper Do not use a computer

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++ DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++ Objective: To Learn Basic input, output, and procedural part of C++. C++ Object-orientated programming language

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? Exercises with solutions. 1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? #include b) What using statement do you always put at the top of

More information

Examination for the Second Term - Academic Year H Mid-Term. Name of student: ID: Sr. No.

Examination for the Second Term - Academic Year H Mid-Term. Name of student: ID: Sr. No. Kingdom of Saudi Arabia Ministry of Higher Education Course Code: ` 011-COMP Course Name: Programming language-1 Deanship of Preparatory Year Jazan University Total Marks: 20 Duration: 60 min Group: Examination

More information

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS July 8 13, 2008 Shumen, Bulgaria TASKS AND SOLUTIONS Day 1 Task 1. TOWERS OF COINS Statement Asen and Boyan are playing the following game. They choose two

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional

More information

Homework 2 Solutions Group B 1- Write a C++ program to read a students score in Statistics and print if he is successful or failing.

Homework 2 Solutions Group B 1- Write a C++ program to read a students score in Statistics and print if he is successful or failing. Homework 2 Solutions Group B 1- Write a C++ program to read a students score in Statistics and print if he is successful or failing. int score; cout>score; if ((score>=60)&&(score

More information

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

Practice test for midterm 2

Practice test for midterm 2 Practice test for midterm 2 April 9, 2 18 1 Functions Write a function which takes in two int parameters and returns their average. (Remember that if a function takes in parameters, it does not need to

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

C C++ C++! "# C++ ++

C C++ C++! # C++ ++ C++ C+ ++ C++ C++.. C++. Brace. " ". /* */ // 31.. Case Sensitive (if, char, while, ). C. char a ; float pi=3.14; : : = ; ; : : . char Character or small integer 1byte short int Short Integer 2bytes int

More information

Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays

Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays Array Array Part 1 - Accessing array - Array and loop arrays must be used with loops - Array and bound checking Be careful of array bound: invalid subscripts => corrupt memory; cause bugs - Array initialization

More information

Score score < score < score < 65 Score < 50

Score score < score < score < 65 Score < 50 What if we need to write a code segment to assign letter grades based on exam scores according to the following rules. Write this using if-only. How to use if-else correctly in this example? score Score

More information

Sample Paper - II Subject Computer Science

Sample Paper - II Subject Computer Science Sample Paper - II Subject Computer Science Max Marks 70 Duration 3 hrs Note:- All questions are compulsory Q1) a) What is significance of My Computer? 2 b) Explain different types of operating systems.

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1 CS150 Intro to CS I Fall 2017 Fall 2017 CS150 - Intro to CS I 1 Chapter 4 Making Decisions Reading: Chapter 3 (3.5 pp. 101), Chapter 4 (4.4 pp. 166-168; 4.5 pp. 169-175; 4.6 pp.176-181; 4.8 pp. 182-189;

More information

DELHI PUBLIC SCHOOL TAPI

DELHI PUBLIC SCHOOL TAPI Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed

More information

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following: Computer Department Program: Computer Midterm Exam Date : 19/11/2016 Major: Information & communication technology 1 st Semester Time : 1 hr (10:00 11:00) Course: Introduction to Programming 2016/2017

More information

C++ Final Exam 2017/2018

C++ Final Exam 2017/2018 1) All of the following are examples of integral data types EXCEPT. o A Double o B Char o C Short o D Int 2) After the execution of the following code, what will be the value of numb if the input value

More information

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/2018 - Fall Q1) Give the correct answer for following: 1. What is the value of x after the following statement? double x; x = 3.0

More information

COS1511_Nov2016_MEMO TUTORIALS CAMPUS 2.1) To add all the even numbers in the array to sum variable, and display sum

COS1511_Nov2016_MEMO TUTORIALS CAMPUS   2.1) To add all the even numbers in the array to sum variable, and display sum 1.1 45 1.2 Twice 1.3 0 1.4 6 1.5 7 1.6 Name.size() 1.7 0 1.8 Classic basic primitive types may include: Character (character, char); Integer (integer, int, short, long, byte) with a variety of precisions;

More information

Exam 3 Chapters 7 & 9

Exam 3 Chapters 7 & 9 Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False

More information

1 Unit 8 'for' Loops

1 Unit 8 'for' Loops 1 Unit 8 'for' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value. 1. What is a pointer? A pointer is a variable that holds the address of another object (data item) rather than a value. 2. What is base address? The address of the nth element can be represented as (a+n-1)

More information

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 03

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 03 CS 102 - Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 03 What is your name?: (0 points) There are two sections: I. Short Questions.........40 points; (40 questions, 1 point each) II.

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

BITG 1113: Array (Part 1) LECTURE 8

BITG 1113: Array (Part 1) LECTURE 8 BITG 1113: Array (Part 1) LECTURE 8 1 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional (1 D)

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Downloaded from

Downloaded from Unit I Chapter -1 PROGRAMMING IN C++ Review: C++ covered in C++ Q1. What are the limitations of Procedural Programming? Ans. Limitation of Procedural Programming Paradigm 1. Emphasis on algorithm rather

More information

Summer II Midterm Part I - Algorithms - 3 points each

Summer II Midterm Part I - Algorithms - 3 points each 1 Summer II Midterm Part I - Algorithms - 3 points each a) RemoveNLastDigits returns the number passed in minus digitstoremove digitstoremove must be positive RemoveNLastDigits(123456,2) ==> 1234 RemoveNLastDigits(123456,5)

More information

Chapter 2. Procedural Programming

Chapter 2. Procedural Programming Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

More information

n Group of statements that are executed repeatedly while some condition remains true

n Group of statements that are executed repeatedly while some condition remains true Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

More information

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),

More information

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (! FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

Structured Data. CIS 15 : Spring 2007

Structured Data. CIS 15 : Spring 2007 Structured Data CIS 15 : Spring 2007 Functionalia HW4 Part A due this SUNDAY April 1st: 11:59pm Reminder: I do NOT accept LATE HOMEWORK. Today: Dynamic Memory Allocation Allocating Arrays Returning Pointers

More information

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011 The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

More information

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure Learning Objectives At the end of this chapter, student should be able to: Understand the requirement of a loop Understand the Loop Control Variable () Use increment (++) and decrement ( ) operators Program

More information

Practice test for midterm 1

Practice test for midterm 1 Practice test for midterm 1 March 5, 2 18 1 Basics of C++ How many comments, directives, declarations, definitions, and statements occur in the following program? /* * myprogram.cpp */ #include

More information

Consider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is " ". Given the function prototype: float test(int,

More information

Write a program that displays all the even integers between 1 and 100, inclusive

Write a program that displays all the even integers between 1 and 100, inclusive Write a program that displays all the even integers between 1 and 100, inclusive for(int i=1;i

More information

Lecture 7: General Loops (Chapter 7)

Lecture 7: General Loops (Chapter 7) CS 101: Computer Programming and Utilization Jul-Nov 2017 Umesh Bellur (cs101@cse.iitb.ac.in) Lecture 7: General Loops (Chapter 7) The Need for a More General Loop Read marks of students from the keyboard

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++] ASSIGNMENT-1 2016-17 CLASS-11 COMPUTER SCIENCE [C++] 1 Consider the following C++ snippet: int x = 25000; int y = 2*x; cout

More information

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program 1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize

More information

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4); 2 3 using std::cout; 4 using std::cin; using std::endl; 6 7 int main(){ 8 int x=21; 9 int y=22; int z=; 11 12 cout

More information

1. FIBONACCI SERIES. Write a C++ program to generate the Fibonacci for n terms. To write a C++ program to generate the Fibonacci for n terms.

1. FIBONACCI SERIES. Write a C++ program to generate the Fibonacci for n terms. To write a C++ program to generate the Fibonacci for n terms. PROBLEM: 1. FIBONACCI SERIES Write a C++ program to generate the Fibonacci for n terms. AIM: To write a C++ program to generate the Fibonacci for n terms. PROGRAM CODING: #include #include

More information

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT LESSON VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT PROF. JOHN P. BAUGH PROFJPBAUGH@GMAIL.COM PROFJPBAUGH.COM CONTENTS INTRODUCTION... Assumptions.... Variables and Data Types..... Numeric Data Types:

More information

LAB 4.1 Relational Operators and the if Statement

LAB 4.1 Relational Operators and the if Statement LAB 4.1 Relational Operators and the if Statement // This program tests whether or not an initialized value of num2 // is equal to a value of num1 input by the user. int main( ) int num1, // num1 is not

More information

CMPE110, Sample questions

CMPE110, Sample questions CMPE110, Sample questions Number : Name :... Q1)Give the correct answer for following: 1. What is the value of x after the following statement? double x; x = 3.0 / 4.0 + 3 + 2 / 4 a. 5.75 b. 4.25 c. 1.75

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1 ELEC 330 1 Structured Programming Control Structures ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne Algorithm Development Conditional Expressions Selection Statements Loops 206_C3

More information

CMPS 221 Sample Final

CMPS 221 Sample Final Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str,

More information

Problem Solving: Storyboards for User Interaction

Problem Solving: Storyboards for User Interaction Topic 6 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Using Parallel Arrays. Parallel Array Example

Using Parallel Arrays. Parallel Array Example Using Parallel Arrays Parallel arrays: two or more arrays that contain related data A subscript is used to relate arrays: elements at same subscript are related Arrays may be of different types Parallel

More information

A Freshman C++ Programming Course

A Freshman C++ Programming Course A Freshman C++ Programming Course Dr. Ali H. Al-Saedi Mustansiriyah University, Baghdad, Iraq November 4, 2018 1 Number Systems and Base Conversions Before studying any programming languages, students

More information

CSCE Practice Midterm. Data Types

CSCE Practice Midterm. Data Types CSCE 2004 - Practice Midterm This midterm exam was given in class several years ago. Work each of the following questions on your own. Once you are done, check your answers. For any questions whose answers

More information

Connecting with Computer Science, 2e. Chapter 15 Programming II

Connecting with Computer Science, 2e. Chapter 15 Programming II Connecting with Computer Science, 2e Chapter 15 Programming II Objectives In this chapter you will: Gain an understanding of the basics of high-level programming languages, using Java and C++ as examples

More information

MODULE 2: Branching and Looping

MODULE 2: Branching and Looping MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written

More information

Final Exam Solutions PIC 10B, Spring 2016

Final Exam Solutions PIC 10B, Spring 2016 Final Exam Solutions PIC 10B, Spring 2016 Problem 1. (10 pts) Consider the Fraction class, whose partial declaration was given by 1 class Fraction { 2 public : 3 Fraction ( int num, int den ); 4... 5 int

More information

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010 The American University in Cairo Computer Science & Engineering Department CSCE 106-08 Dr. KHALIL Exam II Spring 2010 Last Name :... ID:... First Name:... Form - I EXAMINATION INSTRUCTIONS * Do not turn

More information

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 One Dimensional Q1: Write a program that declares two arrays of integers and fills them from the user. Then exchanges their values and display the

More information

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a 3.1. Getting the Address of a Variable Pointers (read Chapter 9. Starting Out with C++: From Control Structures through Objects, Tony Gaddis.) Le Thanh Huong School of Information and Communication Technology

More information

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ 0.1 Introduction This is a session to familiarize working with the Visual Studio development environment. It

More information

Name Section: M/W T/TH Number Definition Matching (6 Points)

Name Section: M/W T/TH Number Definition Matching (6 Points) Name Section: M/W T/TH Number Definition Matching (6 Points) 1. (6 pts) Match the words with their definitions. Choose the best definition for each word. Event Counter Iteration Counter Loop Flow of Control

More information

Chapter 3 Problem Solving and the Computer

Chapter 3 Problem Solving and the Computer Chapter 3 Problem Solving and the Computer An algorithm is a step-by-step operations that the CPU must execute in order to solve a problem, or to perform that task. A program is the specification of an

More information

Exam (june ) Estimated solving time (10 min) screen shot :- to download the open source click the next url :

Exam (june ) Estimated solving time (10 min) screen shot :- to download the open source click the next url : Exam (june - 2012) Estimated solving time (10 min) screen shot :- to download the open source click the next url : www.facebook.com/issrcs Page 1 code : #include #include using namespace

More information

CHAPTER 9 FLOW OF CONTROL

CHAPTER 9 FLOW OF CONTROL CHAPTER 9 FLOW OF CONTROL FLOW CONTROL In a program statement may be executed sequentially, selectively or iteratively. Every program language provides constructs to support sequence, selection or iteration.

More information

Function with default arguments

Function with default arguments Function with default arguments A function can be called without specifying all its arguments. This can be achieved only if the function declaration provides default values for those arguments that are

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information