c++ Solutions eedsohag.epizy.com Ahmed Ali

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

Control Structure and Loop Statements

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

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

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

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

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

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

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.

C++ PROGRAMMING SKILLS Part 2 Programming Structures

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

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

Looping statement While loop

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

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)

Local and Global Variables

CS242 COMPUTER PROGRAMMING

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 ).

Functions. Introduction :

REPETITION CONTROL STRUCTURE LOGO

A Freshman C++ Programming Course

Study Guide for Test 2

C++ Programming Assignment 3

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

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

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

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

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

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

SECOND JUNIOR BALKAN OLYMPIAD IN INFORMATICS

UNIT- 3 Introduction to C++

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

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.

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

Practice test for midterm 2

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

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

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

Score score < score < score < 65 Score < 50

Sample Paper - II Subject Computer Science

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

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

DELHI PUBLIC SCHOOL TAPI

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

C++ Final Exam 2017/2018

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

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

Exam 3 Chapters 7 & 9

1 Unit 8 'for' Loops

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

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

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

BITG 1113: Array (Part 1) LECTURE 8

A First Program - Greeting.cpp

Downloaded from

Summer II Midterm Part I - Algorithms - 3 points each

Chapter 2. Procedural Programming

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

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

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

Fundamentals of Programming CS-110. Lecture 2

6.096 Introduction to C++ January (IAP) 2009

Structured Data. CIS 15 : Spring 2007

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

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

Practice test for midterm 1


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

Lecture 7: General Loops (Chapter 7)

Add Subtract Multiply Divide

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

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 #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. 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.

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

LAB 4.1 Relational Operators and the if Statement

CMPE110, Sample questions

Introduction to Programming EC-105. Lecture 2

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

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

CMPS 221 Sample Final

Problem Solving: Storyboards for User Interaction

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

Using Parallel Arrays. Parallel Array Example

A Freshman C++ Programming Course

CSCE Practice Midterm. Data Types

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

MODULE 2: Branching and Looping

Final Exam Solutions PIC 10B, Spring 2016

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

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

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

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

We can even use the operator << to chain the output request as:

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

Chapter 3 Problem Solving and the Computer

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

CHAPTER 9 FLOW OF CONTROL

Function with default arguments

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

Transcription:

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!.

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: 12 45 78 Your numbers forward: 12 45 78 Your numbers reversed: 78 45 12 : 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

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++)

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;

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;

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;

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;

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;

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 :

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!.

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)

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;

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;

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.

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 -999. for (int input; input!= -999;) cout << "Enter a number (-999 to quit): ";

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 )

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)

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

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