Declaration of Variables int scoreCount; float score, total; char grade; string courseName; bool validInput; // Memory Allocation // Address for each variable // Size for each data type // int : 2 bytes // float : 4 bytes // char : 1 byte // string: # of chars plus one 1
Declaration of Variables. int scoreCount; float score, total; char grade; string courseName; bool validInput; // Memory Allocation // Address for each variable // Size for each data type // int : 2 bytes // float : 4 bytes // char : 1 byte // string: # of chars plus one. - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Declaration of Variablesint scoreCount; float score, total;char grade;string courseName;bool validInput;
// Memory Allocation// Address for each variable// Size for each data type// int : 2 bytes// float : 4 bytes// char : 1 byte// string: # of chars plus one
1
Computing Highest, Lowest, Average
float score;
float total, max, min, avg;
int scoreCount;
// Using one variable to input and process scores
// One score at a time
// When do we need to keep all scores?
// Is my score below or above the average?
// How do we keep all scores?
// Array!
2
Declaration of Arraysfloat scores[100];// 100 floats
char chs[20];// 20 chars
int nums[30]; // 30 integers
string allNames[100];// 100 strings
bool prog3Completed[26];// 26 bool values
3
Memory Allocation for Array
10 15 20 50 . . . . 30 18
nums
int nums[30];// 30 integers// Address: the first element// How many bytes?// 30 * 2: 60 bytes
4
Array Size Must Be Constant Expressionsconst int MAX_SIZE = 30;
int size = 30;
int nums[30];float ArrayA[MAX_SIZE]; // Valid?// Yes!// number of bytes?
float ArrayB[size]; // Valid?// NO!
float ArrayC[MAX_SIZE * 10]; //Valid?// Yes!// number of bytes?
5
Array Index and Array Element
• How to access array elements?
• Use array index to specify array element
• Index of any array begins with 0
6
Array Element and Array Index
10 15 20 50 . . . . 30 18
0 1 2 3 28 29
Array Element
Array Indices
int nums[30];
Array Element
nums[0] nums[1] nums[29]7
Operations on Array Elements
• An array element is the same as a single variable
• Operations on array elements:
Input
Output
Arithmetic operations
Assignment
Comparison
Function parameters
…
8
int nums[30];
// Read an integer and store it in the 1st element of nums.
cin >> nums[0];
// Incement the 1st element of nums by one.nums[0] ++;
// Assign the value of the 1st element of nums to // the 2nd element of nums.nums[1] = nums[0];
// Incement the 2nd element of nums by the value of size.nums[1] += size;
// Set the last element of nums to the value of size.nums[29] = size;
// Assign the value of the 16th element of nums to size.size = nums[15];
9
int nums[30];
// Display 1st element of nums with a width of 9.cout << endl << setw(9) << nums[0];
// Compute the average of the 1st and last elements of // nums and store it in avg.avg = (nums[0] + nums[29]) / 2.0;
// If the 2nd element is not zero, display the float // quotient of 1st element divided by 2nd.if (nums[1] != 0) cout << "Quotient = " << float(nums[0]) / nums[1];
// Call function Largest to find the max value between // the first three elments of nums and store it in Max.// Function prototype: // int Largest(int num1, int num2, int num3);Max = Largest(nums[0], nums[1], nums[2]);