2010 Lab-9

Elements of this lab...
   . pointers
   . arrays
   . functions
   . dynamic memory allocation
   . command-line arguments

Write your week-9 programs in your /2010/9/ folder.
Write your week-10 programs in your /2010/a/ folder.
Week-10 Homework...

Homework #9 has been extended into week-10.

Copy your programs from folder 9 to folder a.

Continue working on them in folder a.

  1. Complete the expander.cpp program.
     We wrote most of it together in class.
     a. Complete the statistics portion at the end of the program.
     b. Run it under valgrind without any "lost memory blocks"
        or memory "leak" errors.

  2. Make your array reverse.cpp program work without a stack,
     but rather by making your reverseArray function recursive.
     See below.

  3. Continue writing your square.cpp program.
     Declare an instance of your Shape struct for each of your shapes.
         Example from my own program...
           Shape circle;
           Shape ring;
           Shape rectangle;

Here is the output of my program.



One method for a rectangle...
  1. instantiate a "Shape rectangle" object
  2. define a center, width, height of your rectangle
  3. get the difference in j from the center.x
  4. get the difference in i from the center.y
  5. if |dx| is equal to half the width,
     check that it is between the top and bottom of rectangle.
  6. if |dy| is equal to half the height,
     check that it is within the left and right of rectangle.

note:  |dx| means dx is a length or distance, which is always positive.
       Use the C++ absolute value function abs(dx) to do this.

You may use your own method also.

Start...
We will work on this program together to start the lab.

Warmup...

for the warmup function, we will import our pointers.cpp
program code from Monday. Then we will make some changes.



larger.cpp
Create an array of integers. You may give it a size of about 20.

Initialize the array by filling it with some random values.

Prompt the user for the number x.

Write a function that accepts three arguments:
  1. an array
  2. the size of the array
  3. a number x.

The function will display all of the numbers in the array that are greater
than the number x.

Use as few [] operators as possible in your code.

sample output...
Lab-9 array largest values. Array values: 73 16 27 25 23 25 46 12 69 1 32 67 50 79 23 76 60 36 52 46 Enter a value please: 70 Array values larger than 70 73 79 76

bigsmall.cpp
Write a program that prompts the user for the size of an array.

1. Declare a float or double array of the user's size.
   Create the array using dynamic memory allocation.

2. Populate the whole array with random values.

3. Send the array to a function that will sort it in ascending sequence.
   The function will not display anything.
   Try not to use the [] operator in your function.

4. Display the 3 smallest and the 3 largest values in the array.

sample output...
Lab-9 extreme array values. How big is the array? 250 3 smallest values: 0.109453 0.21399 0.469181 3 largest values: 99.2463 98.8292 97.3739 deleting the array now. done.


We will do the following 2 things in class together.
Array allocate function

Write a function that dynamically allocates an array of integers.
The function should accept an integer argument indicating the
number of elements to allocate.

The function will return a pointer to the array.

Demonstrate your function in the menu options below.
Command-line arguments

We will control the random number generation from the command-line
of our lab9 program.

Program usage: ./lab9 <optional seed value>

seed value:
    blank or zero means seed with the timer.
    any other numeric value is used as the seed value.
    atoi function used to read the argument value.

Homework programs are below.

week-9 program
expander.cpp

week-10 program
2010/a/expander.cpp

-----------------------------------------------
For homework-10
Please complete the 3-lines at the end of the print.
-----------------------------------------------

Write a program that creates an array using dynamic memory allocation.

1. Ask the user for the size of the array.
   It should be a size of 1, 2, or 3.
   Dynamically allocate the array.

   New item!!!
2. Ask the user for how many values to insert into the array.

3. Fill the array to the user's request.
   The user might enter 5 or 5-million. That's ok.

Display the complete array just before expanding it.

(Display only if the array has less than about 30 values.)

Optional: you may allocate memory to arrays using your get_memory function
          that we wrote in the readarr.cpp program. 

sample output is here:
Lab-9 Array expander -------------------- Enter starting size of array: 1 How many values to insert into array? 25000 49 49 36 49 36 81 30 49 36 81 30 63 66 47 14 49 36 81 30 63 66 47 14 82 28 40 26 8 89 86 8 Final array has 25000 values stored. 15 array expansions were needed. Last array value is 11 at index 24999
Here is how to do the array expansion... You should write a function to do the expansion. The function will have 2 arguments, the full array and its size. The function return-type will be an integer pointer. In the function... 1. Calculate the size of your new expanded array. ---------------------------------------------- It's the size of your current full array times 2. Double the size. 2. Dynamically allocate your new expanded array. --------------------------------------------- 3. Starting at index 0, ------------------- copy all the elements from your full array into the new expanded array. It will only be half full, but that's ok because now there's room to store more values. 4. Delete your old (full) array. ----------------------------- 5. Return the address of the new expanded array. --------------------------------------------- Your function is called whenever you are trying to insert a new data value into your array, but it is already full. No printing from inside your array-expander function!

reverse.cpp

Write the reverse.cpp program.

Write a function that accepts an int array and the number of array elements.

function prototype might look like this...

void reverseArray(int *arr, int n);

The reverseArray function will reverse the order of the array elements.
DO NOT print from inside the reverseArray function!!

-----------------------------------------------
For homework-10
2010/a/reverse.cpp
-------------
Extra credit:
-------------
Do not use a stack.
Make your reverseArray function recursive.
You are allowed to add one argument.
Something like this...
   void reverseArray(int *arr, int n, int idx);

Recursion will simplify this.
Your function should need just 5-lines of code.
-----------------------------------------------

Write another function...

void displayArray(int *arr, int n);

This function must be called to show the array elements.

Demonstrate your functions in a well-written program.


format of output:
Lab-9 Reverse an array ---------------------- Enter size of array: 20 forward: 0 4 7 4 7 2 2 7 5 2 0 5 6 5 6 9 1 7 8 8 reverse: 8 8 7 1 9 6 5 6 5 0 2 5 7 2 2 7 4 7 4 0

caps.cpp

Capitalize all words

Write a function that takes a sentence and capitalizes the first letter
of each word. The function accepts a C-string as an argument.
The number of words that were capitalized is returned by the function.
Write the function using pointers please.

Function prototype:

int allCaps(char *sentence);

How to do it...

1. Declare a C-string variable large enough to hold a sentence
   that the user might enter.

2. Ask the user to enter a sentence into the C-string.

3. Send the sentence to your allCaps function.

Do not print anything from inside the allCaps function!!!
format of output:
Lab-9 Capitalize words ---------------------- Enter a sentence: this is a test of the caps program. This Is A Test Of The Caps Program. 8 words capitalized.
You may approach this function in the following way: 1. Define a pointer to point to the start of the sentence. 2. Define a boolean flag that indicates a space was found. Set it to true. 3. Start a while-loop that will continue until the pointer indicates a null-terminator. Use pointer-arithmetic to advance one character at a time in the loop. 4. If the boolean flag is true, then change the current character to upper case and set the flag to false. 5. When a space is found, set the flag to true, and continue looping. Call the toupper() function.