Review of C++ Basics.

Slides:



Advertisements
Similar presentations
Functions Prototypes, parameter passing, return values, activation frams.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
CS Class 16 Today Announcements Review for Exam
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
CS150 Introduction to Computer Science 1
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Functions:Passing Parameters by Value Programming.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
#include using namespace std; void main() { cout
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Chapter 7 Arrays. Problem: Assume that an instructor has given an exam in class and wants to find the average mark and the highest mark. Write a complete.
Arrays.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Programming Functions: Passing Parameters by Reference.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
ITEC 320 C++ Examples.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Engineering Problem Solving with C++, Etter
C++ Arrays.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS150 Introduction to Computer Science 1
Counting Loops.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
Arrays of Two-Dimensions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Aggregate Functions.
Introduction to Functions
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Presentation transcript:

Review of C++ Basics

Problem Solving With Arrays & Functions Write a program that will read data, sum the numbers, compute the average and find the maximum value Read data from the console Read data from a file

Program /* Program to process exam grades */ #include <iostream> using namespace std; const int SIZE = 50; /* Function Prototypes */ void readdata(int [ ], int &); int sumarray(int [ ], int); double avgarray(int [ ], int); int findmax(int [ ], int);

Main Program int main() { int num; int mark[SIZE]; double avgmark; int numabove,numbelow,numequal,hi_mark; // call function to read the marks and print the array readdata(mark,num); // print the mark array for (count = 0; count < num; count++) cout<<“mark[“<<count<<“]=”<<mark[count] << endl; // find and print the average mark avgmark = avgarray(mark,num); cout <<endl<< "The average is “ << avgmark <<endl; // find and print the highest mark hi_mark = findmax(mark,num); cout << "The highest mark is “ << hi_mark << endl; return 0; }

Function readdata() The Function readdata(): /* Function readdata() * Input: numbers - an array to be filled with n numbers * the parameters are uninitialized upon entry * Process: reads n and reads n values from the console into the array * Output:the filled numbers array */ void readdata(int numbers[ ], int &n) { cout << "Enter the number of marks: "; cin >> n; for (int count = 0; count < n; count++) { cout << "Enter a mark: "; cin >> numbers[count]; } return;

Function sumarray() /* Function sumarray() * Input: numbers - an array of n integers * Process: finds the sum of the first n elements in the numbers array. * Output: returns the sum to the calling function. */ int sumarray(int numbers[ ], int n) { int sum=0; for (int count = 0; count < n; count++) sum += numbers[count]; return(sum); }

Function avgarray() /* Function avgarray() * Input: numbers - an array of n integers * Process: calls sumarray() to find the sum of the first n elements and then divides by n to find the average. * Output: returns the average to the calling function. */ double avgarray(int numbers[], int n) { return ((double)sumarray(numbers,n)/n); }

Finding the Largest Element of an Array /* Function findmax() * Input:numbers - an array of n integers * Process:finds the largest value in the array * Output:returns the maximum value within the array */ int findmax(int numbers[], int n) { int largest_so_far; largest_so_far = numbers[0]; for (int count = 1; count < n; count++) if (largest_so_far < numbers[count]) largest_so_far = numbers[count]; return(largest_so_far); }

Reading input data from a file In order to read from a file we need to declare the input file before main() #include <fstream> Ifstream infile; Then in main, open the file so it can be used in the function infile.open(“c://marks.txt”); /* Function readdata() * Input: numbers - an array to be filled with n numbers * the parameters are uninitialized upon entry * Process: reads n values from a file into the array * Output:the filled numbers array */ void readdata(int numbers[ ], int &n) { n=0; while (infile>>numbers[n]) n++; return; }