Quick Exercise
Write a C++ program to read in 7 characters into an array
and then print them out again. You can use the book if you wish.
Style: the simplest thing that works.
[ 03.cpp ]
Exercise on RAM
Either
Case
Chapter 7 pages 269 -- Multidimensional arrays
Could you please give a simple example of a multidimensional array?
A Tic-Tac-Toe Board.
Any mathemtaical matrix.
The computers screen is a large, 2-dimensional array of pixels.
Ninety-nine bottles of beer on 11 shelves -- 9 bottles a shelf.... (a poster I have at home).
A keyboard.
The apartments in a high-rise building.
Any more?
Chapter 7 pages 334-335 -- Arrays
Do arrays make coding a program any easier?
No.... it just makes soem programs possible that are impossible without them.
Nothing makes programming easy:-(
Thinking ahead, being very careful, knowing the language, and knowing computer science can mke it easier.
We use arrays because a lot of data is in arrays. It is rare to find an application with
a single item... it s nearly always about a number of similar ones.
Chapter 7.5 pages 356 -- passing arrays to functions
How bad can logic errors mess up your program?
Totally. Worst case scenario: dead operating system.... or worse a hacker
running your computer remotely.
Chapter 7 pages 369 -- multidimensional array
Is there a certain sequence numbers have to be input in a multidimensional array to have the highest value in first?
No. All arrays start with subscript 0. The values in the arrays are what you put there. If you need the larger numbers first then you must do it yourself -- either in the initial values, or (more common) by sorting the data.
Chapter 7 pages 337-339 -- Using Arrays
In Figure 7.4 Line 15, where it cout's an Element what is the use of the "13" in setw(13) is it supposed to be the specified with?
The setw is a input-output manipulator that sets the width of the next input or output.
In this case setw(13) it means that out put data is padded or trncated to fit in 13 characters
on the display.
Chapter 7 pages 334-335 -- Subscript
Do other languages use c[i] for c subscript of i?
Yes. Nearly al of them. It started with Algol 60, in 1960.
Chapter Chapter 7 pages 335-338 -- Arrays
Can you use any other operators besides brackets to enclose the subscript of an array ?
No.
Chapter 7 pages 345 -- Arrays
Could you explain lines 25 & 26 of Fig. 7.10, why does the author use "int roll"
The variable roll is a counting variable. It's purpose is to count the number of times the die has been rolled. They have been careful to use a good name -- "roll" -- so we can figure this out. A bad programmer might have written
for( int foo=1; foo < 6000000; foo++)which would make the purpose less clear.
Chapter 7 pages 337 -- rewrite array
Can this be rewritten in a different way
type arrayName[arraySize];
I don't think so. This is that standard way to declare an array for the last 30 years...
Chapter 7 pages 369-371 -- Multidimensional Arrays
Can you elaborate on multidimensional arrays? What exactly are the "for" statements used for in page 371?
For loops are used to scan acrross arrays so that the statements inside the loops can do something to each element in turn.
The lines 30-37 have exactly this form (on page 370). If we have time we should try to work out what these nested loops do, step by step.
Handling a 2-dimensional array often has code like this
For each row in turn
{
handle start of row;
For each item in row ....
handle end of row;
}Notice the loop inside the loop.
Chapter 7 pages 379 - 384 -- Arrays and Vectors
Simply put how do you know when you should use a Vector instead of an array and vice versa?
As a rule use vectors always -- they are safer and have ferwer nasty surprises.
Chapter 7 pages 334-392 -- Arrays and Vectors
When is it beneficial to use vectors rather than arrays or vice versa?
Normally.
Chapter 7 pages 334-380 -- Arrays
Are arrays ever useful?
When you know the number of items in that you need. When you need to make the program faster. When you want to initialize a fixed number of items.
Chapter 7 pages 379 -- vectors vs. arrays
What are the performance differences between vectors and arrays?I know they would be neglible on small things but what about something computer intensive(3-d games,encoding,rendering)?
Arrays are used for High-Performance Computing -- especially rendering, games, etc. Special chips can work in parallel on arrays. The major cost is that you have to be a lot more careful of the programming with arrays.
Chapter 7 pages 354-358 -- Passing Arrays to Functions
Can you explain how passing array arguments to a function works?
An array is a block of storage. When an array name is put in a function header then the address of the block of storage is given to the function. Inside the function operations on the array are actually done to the original block of storage.
Chapter 7.7 pages 365-366 -- Arrays/Linear Search
can you elaborate on how to use "Linear Search" using vectors and explain more in depth on what "Linear Search" does and how to implement it?
Linear search is used any time we have a pretty random set of items of data and need to find one of them. Exactly the same code is used for arrays, vectors, files, etc.
No.
Chapter 7 pages 370 -- Multidimensional Arrays
In a function header, why isn't the first subscript required, but all following subscripts required.
The purpose of the first subscript in a declaration is to tell the compiler how much space to allocate to the array. The number is not used for calculations or checking subscripts inC/C++.
In a function header arrays are passed by reference and so no data is allocated. The address of the first item ([0]) is passed of an existing block of RAM. Thus the function header does not need to be given the size of the storage. It is therefore omitted.
The rest of the numbers are used to calculate the position of elements in RAM and so are essential.
Chapter number7 pages 379-383 -- vectors
how to declare vectors?
Like this
vector < T > v;where the vector v is empty but can hold items of type T. or
vector < T > v (s);where the vector named v will have space for s elements of type T.
Vectors -- declare size first or grow
In general, is it better to declare the size of a vector initially or is it better to just use pushback or popback?
The is no general rule. Sometimes the program has no items to start with and so grows the vector by adding items as needed. Sometimes we know the first 5 items (say) and set the size in the declaration and use subscripts to load the five values. This is rare in my experience. It s also more confusing but may be a little bit faster. It is confusing because you switch from using existing elements to pushing and popping. You have to make the transition at the write time. It is faster because the compiler can pre-allocate the block of space.
Chapter 7 pages 380-383? -- v.pop_back()
Is it possible to "pop" multiple vector elements using v.pop_back(4), or must I use a loop?
You have to do it with a loop. This
vector::pop_back( int n);is not defined in <vector>.
Chapter 8 pages 414 -- Passing Arguments to functions by reference with pointers
In the book it claims that there are three ways in c++ to pass arguments to a function 1. pass by reference with reference arguments, pass by reference with pointer arguments, as well as pass by value...is their any other more complicated way to pass an argument to a function?
The more complex ways of passing data to and from functions are really combinations of the above.
Lab -- Drawing UML Diagrams
[ lab02.html ]
A Quick Introduction to the UML
[ uml0.html ]
Next Class
[ 04.html ]
Project 1 due start of Next class
. . . . . . . . . ( end of section CSci202 Computer Science II, Session 03 -- Arrays and Vectors) <<Contents | End>>
Abbreviations