Home >  T.A >  cs10 ( Spring 2002 ) >  Lab 10

  CS10 Sec#24, #29, #30 : Topics covered in Lab #10
T.A: Demetris Zeinalipour
(Spring 2002)
Print this Page   
 [  Working with arrays example  ::   Exercise #9 ]



Working with Arrays

Initialize array (at compile time):

int myarray[3] = {4, 19, 2};

How to Initialize all slots of an array to a specified value:

int myarray[12][31] = {{0}};
Notice that this array has 12*31=372 slots (or positions). By this all positions will be set to a value of 0.

Print Array on screen
/* Print the passed array on the screen
* @return : void
*/
void printArray(int myarray[])
{
  cout << "Array: [";
  for (int i=0; i< SIZE; i++)
  {
    cout << myarray[i];

   // Don't print a comma after the last item
    if (i!=SIZE-1)
    { cout << ", "; }
  }
  cout << "]\n";
}
Top


Lab 10
New! - Download lab10.exe (win32) here and see how your program should correspond to various inputs.
See Solution - C++ solution here (will be available on Saturday since labs are running until friday)


Write a program that deals with arrays. Write the following functions and then use the given program, filling in the appropriate function calls. Do not change any code in the given program.
Write a function that takes as a parameter an array of floating-point values and lets the user set all the values in the array.
Write a function that takes as parameters a one-dimensional array of floating-point values, a two-dimensional array of floating-point values, a character representing either row or column ('r' for row and 'c' for column), and an integer representing the row or column position. Copy the values in the one-dimensional array into the specified row or column of the two-dimensional array.
Write a function that takes as a parameter a two-dimensional array of floating-point values and prints them out nicely.


#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE = 5;

// INSERT FUNCTION PROTOTYPES HERE

void main()
{

 double values[SIZE] = {0};
 double table[SIZE][SIZE] = {{0}};

 char choice, row_col;
 int position;

 do
 {

  // CALL FUNCTION TO SET VALUES IN ONE-DIMENSIONAL ARRAY

  do
  {
   cout << "Copy values to specified row (r) or column(c)?  ";
   cin >> row_col;

  } while(row_col != 'r' && row_col != 'c');


  cout << "Enter row/column position (0-4):  ";
  cin >> position;


  while(cin.fail() || position < 0 || position >= SIZE)
  {
   cin.clear();
   cin.ignore(80, '\n');
   cout << "Enter row/column position (0-4):  ";
   cin >> position;
  }

  // CALL FUNCTION TO COPY VALUES FROM ONE-DIMENSIONAL

  // ARRAY TO TWO-DIMENSIONAL ARRAY IN GIVEN LOCATION

  // CALL FUNCTION TO PRINT TWO-DIMENSIONAL ARRAY NICELY

  cout << "Set more values: (y/n):  ";
  cin >> choice;

 } while(choice == 'y');

}


Notice: Be sure to put all your information (login, lab section, …) at the top of your program. Your lab programs DO NOT get turned in electronically. I will come around and check you off individually.

Top