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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

An introduction to pointers in c
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Pointers Applications
Arrays.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Arrays and Pointers in C Alan L. Cox
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Homework due 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.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Lives and Scoring Games Programming in Scratch. Games Programming in Scratch L2 Lives and Scoring Learning Objectives Define a variable Understand the.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Arrays.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
INC 161 , CPE 100 Computer Programming
Arrays Chapter 7.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
4. Java language basics: Function
EGR 2261 Unit 10 Two-dimensional Arrays
Arrays Low level collections.
COSC 220 Computer Science II
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Passing Arguments to a Function
Arrays in C.
Conditionals & Boolean Expressions
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Arrays An Array is an ordered collection of variables
Yong Choi School of Business CSU, Bakersfield
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
One-Dimensional Array Introduction Lesson xx
Organizing Memory in Java
Chapter 8 Slides from GaddisText
int [] scores = new int [10];
Engr 0012 (04-1) LecNotes
Lecture 18 Arrays and Pointer Arithmetic
Arrays 6-Dec-18.
Code::Block vs Visual C++
Introduction To Programming Information Technology , 1’st Semester
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Starting Out with Programming Logic & Design
CS2011 Introduction to Programming I Arrays (I)
Arrays Chapter 7.
Seoul National University
Multidimensional Arrays
Building Java Programs
Multi-Dimensional Arrays
INC 161 , CPE 100 Computer Programming
Arrays in Java.
Homework Continue with K&R Chapter 5 Skipping sections for now
Data Structures & Algorithms
Arrays 2-May-19.
While Loops in Python.
SPL – PS2 C++ Memory Handling.
Week 7 - Monday CS 121.
Presentation transcript:

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.

C++ Arrays – Part I

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)

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;

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};

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.

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

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

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

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

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?

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

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, 100000

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}

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