Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.

Similar presentations


Presentation on theme: "Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell."— Presentation transcript:

1 Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell you what the operator is, then tells the user if they were correct or incorrect and, if incorrect, gives the correct answer. The program should loop asking the user if they want another problem to solve and stop when they don’t. It should also keep track of the number of correct and incorrect answers and report the score when the user is finished.

2 C++ Arrays – Part I

3 Compile time allocation
This is something Java does not offer Arrays are always allocated on the heap Allocate an array of 20 ints int array[20]; Indices run from 0 to 19 (as in Java) Contents are UNITITIALIZED!!! Actually, they are arbitrarily initialized (memory locations always have values)

4 Check it out Create/build/run the following program in VisualStudio
#include "stdafx.h" #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { int uninitialized[5]; for (int i = 0; i < 5; ++i) { std::cout << uninitialized[i] << std::endl; } return 0;

5 Initialization You can do the usual (Java-like) things or
int initialized[5]; for (int i = 0; i < 5; ++i) { initialized[i] = 0; } or int initialized[5] = {0, 0, 0, 0, 0};

6 Initialization You can also do this, which is [sort of] allowed in Java but does something quite different int initialized_0[5] = {0}; or int initialized_1[5] = {1}; Add these lines to your program and print out the contents of each array to see what happens.

7 Number of elements in an array
There is no .length attribute as in Java because arrays are not references To get the number of elements in an array you must do some operations int initialized[5]; int bytes = sizeof(initialized); The sizeof() operator returns the number of bytes in the data type What will the value of the variable bytes be? Try it and see

8 Number of elements in an array
To get the number of elements you need to divide the total number of bytes in the array by the number of bytes in each element int initialized[5]; int length = sizeof(initialized) / sizeof(initialized[0]); Try it

9 Multi-dimensional arrays
In Java a 2 dimensional array is a 1 dimensional array of 1 dimensional arrays Not so in C++ Here it’s a contiguous block of memory with index markers

10 Multi-dimensional arrays
int twoD[3][2]; twoD[0][0] twoD[0][1] twoD[0][2] twoD[1][0] twoD[1][1] twoD[1][2] n n + 1 * sizeof(int) n + 2 * sizeof(int) n + 3 * sizeof(int) n + 4 * sizeof(int) n + 5 * sizeof(int) memory address access

11 Multi-dimensional array initialization
int twoD[3][2] = {0, 1, 2, 3, 4, 5}; int twoD[3][2] = {{0, 1}, {2, 3}, {4, 5}}; int twoD[3][2] = {0, 1}; int twoD[3][2] = {{0, 1}}; int twoD[3][2] = {{0, 1, 2}}; // error…why?

12 Multi-dimensional array lengths
int twoD[3][2]; int rows = sizeof(twoD) / sizeof(twoD[0]); int cols = sizeof(twoD[0]) / sizeof(twoD[0][0]);

13 Homework – Part I Test the random number generator
Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average of the array contents Do this for n = 10, 100, 1000, 10000,

14 Homework – Part II Create a 2D array of m x n ints
Fill the array with random numbers between 0 and 100 Compute and report the average of the array contents Do this for all combinations of m = {1, 10, 100} and n = {1, 10, 100, 1000}

15 Homework – special instructions
Do not use any hard coded values or constants when calculating loop indices Use the techniques discussed for computing the number of elements in rows/columns of an array Due Thursday, next class meeting – I’ll look at your results in class


Download ppt "Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell."

Similar presentations


Ads by Google