Assignment due - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Assignment due

Description:

Compile time allocation. This is something Java does not offer ... There is no .length attribute as in Java because arrays are not references ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 16
Provided by: d1176
Category:

less

Transcript and Presenter's Notes

Title: Assignment due


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 dont. 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 array20
  • 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 ltiostreamgt
  • int _tmain(int argc, _TCHAR argv)
  • int uninitialized5
  • for (int i 0 i lt 5 i)
  • stdcout ltlt uninitializedi ltlt stdendl
  • return 0

5
Initialization
  • You can do the usual (Java-like) things
  • int initialized5
  • for (int i 0 i lt 5 i)
  • initializedi 0
  • or
  • int initialized5 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_05 0
  • or
  • int initialized_15 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 initialized5
  • 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 initialized5
  • int length sizeof(initialized)
  • / sizeof(initialized0)
  • 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 its a contiguous block of memory with index
    markers

10
Multi-dimensional arrays
  • int twoD32

11
Multi-dimensional array initialization
  • int twoD32 0, 1, 2, 3, 4, 5
  • int twoD32 0, 1, 2, 3, 4, 5
  • int twoD32 0, 1
  • int twoD32 0, 1
  • int twoD32 0, 1, 2 // errorwhy?

12
Multi-dimensional array lengths
  • int twoD32
  • int rows sizeof(twoD) / sizeof(twoD0)
  • int cols sizeof(twoD0) / sizeof(twoD00)

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

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 Ill look at
    your results in class
Write a Comment
User Comments (0)
About PowerShow.com