MT262A Review.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Introduction to C Tom Chao Zhou CSC2100B Data Structures Tutorial 1.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
ITEC 320 C++ Examples.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Control Structures RepetitionorIterationorLooping Part I.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Introduction to C Zhengwei Yang CSC2100 Data Structures Tutorial 1.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Basic concepts of C++ Presented by Prof. Satyajit De
CSC111 Quick Revision.
REPETITION CONTROL STRUCTURE
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 1.2 Introduction to C++ Programming
while Repetition Structure
C++ Programming: CS150 For.
EDUSITE Introduction to C manjari2707.wordpress.com.
Chapter Topics 11.1 Introduction to Menu-Driven Programs
Arrays Part-1 Armen Keshishian.
Multi-dimensional Array
C++ Arrays.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
Student Data Score First Name Last Name ID GPA DOB Phone ...
Control Statement Examples
Lab 1 Introduction to C++.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++.
Arrays Skill Area 315 Part A
Compound Assignment Operators in C++
Yuanming Yu CSC2100B Data Structures Tutorial 1
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Arrays Kingdom of Saudi Arabia
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Lab 1 Introduction to C++.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Reading from and Writing to Files
Functions Divide and Conquer
File I/O in C++ I.
Reading from and Writing to Files
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

MT262A Review

Q1 Write a C++ program to print the sequence 0 , 100 , 200 , 300 , …………….., n, where the value of n is entered by the user.

int main(int argc, char* argv[]) { int N , int=I, cout << "Enter N : " ; cin >> N ; For (I=0; I<=N, I=I+100) cout << I <<“ “; getchar(); return 0; }

Q1 Write a C++ program to compute and print the average of the sequence 0 + 100 + 200 + 300 + ……………..+ n, where the value of n is entered by the user.

int main(int argc, char* argv[]) { int N , I=1, C = 0; float SUM = 0, AV; cout << "Enter N : " ; cin >> N ; For (I=0; I=N, I=I+100) SUM =SUM + I ; C = C + 1 ; } AV = SUM / C ; cout << "\n Average = " << AV ; getchar(); return 0;

Q2 Write a C++ program to calculate the following equation and then print the result of the A  A = 1.5 * (B - C) /( D + E)

int main(int argc, char* argv[]) { float A , B , C , D , E ; cout << "Enter B : " ; cin >> B ; cout << "Enter C : " ; cin >> C ; cout << "Enter D : " ; cin >> D ; cout << "Enter E : " ; cin >> E ; A = 1.5 * (B - C) /( D + E); cout << "\n A = " << A ; getchar(); return 0; }

Q3 Write a program that will perform the following tasks: a. Display the following menu: 1) Add 2) Delete 3) Update 4) Exit b. Ask the user to enter his choice. c. The program will print “You want to ” + the needed task. For example, if the user enter the value 1 then the program will print "You want to add" d. If the user enter a number which is not available then the program will display the message "Invalid number" e. The program will keep running until the user input 4 for exit.

default: cout <<"Invalid number"; } }while (op !=4); int main(int argc, char* argv[]) { int  op ; do { cout << "\n" << "1) Add"       ; cout << "\n" << "2) Delete"     ; cout << "\n" << "3) Update"      ; cout << "\n" << "4) Exit"       ; cout << "\nEnter your choice, please:\n" ; cin >> op; switch(op) { case 1: { cout << "\n Your want to Add“; break; } case 2: { cout << "\n Your want to Delete“; break; } case 3: { cout << "\n Your want to Update“; break; } default: cout <<"Invalid number"; } }while (op !=4); }

Q4 Write a program that will perform the following tasks: a. Ask the user to enter total amount of sales. b. The program will calculate the discount based on the following categories: 20% : amount of sales > = 10000 15% : amount of sales from 5000 to <10000 10% : amount of sales from 1 to <5000 c. Display the total amount of sales before and after discount in addition to the amount of discount. d. The program will keep running until the user input zero.

int main(int argc, char* argv[]) { int amtofsals , amtofdis ; cout << "\n Enter the amount of your purchases:" ; cin >> amtofsals; while (amtofsals !=0) { if (amtofsals >= 10000) amtofdis= amtofsals*0.2; else if (amtofsals >= 5000) amtofdis= amtofsals*0.15; else amtofdis= amtofsals * 0.1; cout << "\n Amount before discount: " << amtofsals ; cout << "\n Discount: " << amtofdis; cout << "\n Amount after discount: " << amtofsals - amtofdis ; cout << "\n \n Enter the amount of your purchases:" ; cin >> amtofsals; } getchar();

Q5 Write a C++ program which reads two integer numbers and calculate their product using function.

#include <iostream>; int mult ( int x, int y ); int main() { int x; int y; cout<<"Please input two numbers to be multiplied: "; cin>> x >> y; cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n"; } int mult ( int x, int y ) { return x * y;

Q6 Write a C++ program which store the values of two arrays {5, 10, 15}; {2, 4, 6, 8, 10}; and then print them using function.

// arrays as parameters #include <iostream> void printarray (int arg[], int length) int main () { int firstarray[] = {5, 10, 15}; int secondarray[] = {2, 4, 6, 8, 10}; printarray (firstarray,3); printarray (secondarray,5); return 0; } { for (int n=0; n<length; n++) cout << arg[n] << " "; cout << "\n";

Q7 Write a C++ program to initialize the data of an employee which has (id number = 1, age=22, salary=12000.21) using structure.

struct database { int id_number; int age; float salary; }; int main() database employee; //There is now an employee variable that has modifiable // variables inside it. employee.age = 22; employee.id_number = 1; employee.salary = 12000.21; }

Q8 Write a C++ program to store the values:11, 22,33 in a file named data.txt.

// - Write out to a file. #include <iostream> #include <fstream> int main() { int num1 = 11; int num2 = 22; int num3 = 33; // Step #1 - Declare an output file object file. ofstream outFile; // Step #2 - Open the output file destination. outFile.open(“data.txt"); // Check for success. if (outFile.fail()) { cout << "Error opening \"data.txt.\" for output.\n"; return 1; } // Step #3 - Write to file. outFile << num1 << endl << num2 << endl << num3 << endl; // Successful message. cout << "The data has been written to the file.\n\n"; // Step #4 - Close the output file object. outFile.close(); return 0; }