Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem: A company employs a group of fifty salespersons (with reference numbers 0 - 49) who are paid commission if their individual sales exceeds two-thirds.

Similar presentations


Presentation on theme: "Problem: A company employs a group of fifty salespersons (with reference numbers 0 - 49) who are paid commission if their individual sales exceeds two-thirds."— Presentation transcript:

1 Problem: A company employs a group of fifty salespersons (with reference numbers 0 - 49) who are paid commission if their individual sales exceeds two-thirds of the average sales of the group. Write a program to read in the sales of each of the 50 salespersons and print out the reference number of those salespersons who qualify for commission together with their sales. read(sales0, sales1, …, sales49) calculate commission level if(sales0 > comlevel) write(0, sales0) if(sales1 > comlevel) write(1, sales1) if(sales49 > comlevel) write(49, sales49) …

2 Consider the single data item, sales, to have 50 components: sales 0 sales 1 … sales 49 Denote these components by sales[0] sales[1] … sales[49] Statement part of solution might be { sum = 0.0; for(i = 0; i < 50; i++) { cin >> sales[i]; sum = sum + sales[i]; } comlevel = 2.0/3 * (sum / 50); for(i = 0; i < 50; i++) if(sales[i] > comlevel cout << i << sales[i]; } Solution assumes 2 language features: ability to denote a group of variables by a single identifier, sales ability to distinguish a particular variable or component of a group

3 Problem: Reverse a string of characters Case 1: Length known abcd dcba char ch[4]; for(i = 0; i < 4; i++) cin >> ch[i]; for(i = 3; i >= 0; i--) cout << ch[i];

4 Problem: Reverse a string of characters Case 2: Length unknown abadce … ba* char stack[30]; // max no. of chars char chr; i = 0; cin >> chr; while(chr != '*') { stack[i] = chr; i++; cin >> chr; } while(i != 0) { i--; chr = stack[i]; cout << chr; }

5 Problem: Write a program to input a string of characters and output whether or not the string is a PALINDROME e.g. LEVEL, MADAM, REDDER, ABLE WAS I ERE I SAW ELBA Stack[i] I = i-1; J = 0; while(J < I && Stack[I] == Stack[J]) { J++; I--; } if(J >= I) cout << "String is a palindrome"; else cout << "String is NOT a palindrome"; …… *

6 Processing Student Grades const int NUMSTUDENTS = 5; char grade[NUMSTUDENTS]; // array of // letter grades int idNbr; // Student ID# grade[0] F grade[1] B grade[2] A grade[3] C grade[4] F for(idNbr=0; idNbr<NUMSTUDENTS; idNbr++) cout << grade[idNbr]; gives cout << "Student " << idNbr << "Grade " << grade[idNbr] << endl; gives

7 Declaring an Array Variable const int BUFFERSIZE = 256; const int DICESUMS = 11; char Buffer[BUFFERSIZE]; // constant integer dimension int DiceFreq[DICESUMS + 1]; // constant integer expression, // used as dimension int numItems = 10000; // integer variable string Inventory[numItems]; // NOT valid - numItems is not // a constant An array is a variable. An array has parts and it is structured, but it is still just a variable. Every C++ array has a name, and as always that identifier must be declared before it may be used. The declaration of an array must specify the type of element the array stores and the dimension (number of cells) the array will have. The elements may be of any type at all, but the dimension must be a positive integer constant or an expression that evaluates to an integer constant.

8 Accessing the Individual Cells of an Array We refer to the individual positions in an array as cells or elements. Each cell is named by giving the name of the array, followed by an integer indicating a particular position within the array; the integer is enclosed in square brackets. The integer is often called the index of the element. A[0] A[1]A[2]A[3]A[4]A[5]A[6]A[7]A[8]A[9]A[10]A[11]A[12]A[13] A1313 74343 531919 22323 2929 31311 1717 4747 4141 Here: A[3] == 5 and A[7] == 23. The individual elements of an array can be used in any way that a simple variable of that type may be used. So we could write: A[3] = 51; // stores the value 51 in cell #3 of A if (A[0] < A[5]) { // compares element #0 and element #5... }

9 Array Declarations and Memory A[0]A[1]A[2]A[3]A[4]A[5]A[6] Memory ?? Logically, the valid index values range from 0 to 6 (the dimension minus 1). As with any variable, declaring an array does not cause any automatic initialization of the memory allocated for it. (That's why the cells above are filled with question marks.) One of the Deadly Sins of Programming is failing to initialize variables before they are used. That is especially important with array variables. We will return to this issue shortly. If we have the declaration: char A[7]; then at runtime memory will be allocated for the array variable A. Since A has 7 cells, each of type char, and a char is stored in one byte of memory, A will require 7 bytes of memory. These will be allocated contiguously (as a single chunk) and the cells will then be stored in the order:

10 An array may be initialized with a simple for loop: Initializing an Array A[0]A[1]A[2]A[3]A[4]A[5]A[6] Memory %%%% The for loop counter, Idx, is used as the array index within the loop body. Idx is started at 0 and increased by 1 on each pass. The loop terminates when Idx equals the dimension of the array. const int SIZE = 7; char A[SIZE]; int Idx; for (Idx = 0; Idx < SIZE; Idx++) { A[Idx] = '%'; } The loop design guarantees that each cell of the array will be accessed, in turn, and that the loop will stop before an invalid array index is reached. This is a standard pattern for array processing. Be sure you understand it.


Download ppt "Problem: A company employs a group of fifty salespersons (with reference numbers 0 - 49) who are paid commission if their individual sales exceeds two-thirds."

Similar presentations


Ads by Google