EECS168 09:Lab9

From ITTC
Jump to: navigation, search
Navigation
Home
Information

Syllabus
Schedule
Lecture Notes
Exam Reviews

Classwork

Labs
Homework
Submitting Work

Objectives

  • Introduction to Arrays
  • Using Arrays as Function Arguments
    • Using array index variables as Function Arguments
    • Using entire arrays as Function Arguments

Introduction to Arrays

An array is a collection of data of the same type. The individual variables that together make up the array are referred to as indexed variables or elements or subscripted variables of the array. You can find detailed explanation about Arrays starting on page 376 of your book. The following code is an example for using arrays. The code gets 5 scores from the user and stores them in an array named score of size 5. Copy the following code into array.cpp.

# include <iostream>
using namespace std;
int main()
{
      int i, score[5];
      cout<<"Enter 5 Scores:\n";

      for(i=0; i<5; i++)
      {
        cin>>score[i];
      }
      
      cout<<score[4];
      cout<<endl;

      for(i=3; i>=0; i--)
      {
        cout<<score[i];
        cout<<endl;
      }
return 0;
}

Comprehend how the values are stored in the elements of the array.

Using Arrays as Function Arguments

Both array indexed variables and entire arrays can be used as function arguments. The kind of formal parameter which is an entire array is known as array parameter. Copy the following code to array_parameter.cpp. This code is to read 5 numbers into an array and check whether the the number is multiple of 2,3 and 5 and display accordingly. Execute the code as it is and comprehend the execution of the program and then uncomment the commented parts and fill in the appropriate code to check whether the number is multiple of 3 and 5.

#include <iostream>
using namespace std;

void MultipleOfTwo(int a[], int size, int divisor);
//void MultipleOfThree(int a[], int size, int divisor);
//void MultipleOfFive(int a[], int size, int divisor);

int main()
{
        int SizeOfNumbers = 5;
        int i, Number[5];
        cout<<"Enter the Numbers:\n";
        int b[3] = {2, 3, 5};

        for(i=0; i<5; i++)
        {
                cin>> Number[i];
        }
        MultipleOfTwo(Number, SizeOfNumbers, b[0]);
        //Function call  to check whether number is multiple of 3
        //Function call  to check whether number is multiple of 5

        

        return 0;
}

void MultipleOfTwo(int a[], int size, int divisor)
{
        int k;
        for(k = 0; k < size; k++)
        {
                if(a[k]%divisor == 0)
                {
                cout<<a[k]<<" is multiple of "<<divisor<<endl;
                }
                else
                continue;
        }
}

//Function definition of MultipleOfThree

//Function definition of MultipleOfFive

Construct your own program from scratch

Copy the following text into a file called numbers.dat:

11 21 32 45 52 62 71 81 92 10 
95 87 71 66 54 55 34 22 11 17
85 71 61 52 46 37 28 18 91 13

Write a program even_odd.cpp. Program Requirements:

  • Read the numbers from the numbers.dat file and stores in an array named number of size 30.
  • Write a function named evenorodd that has number as its array argument. This function should check whether the number is odd or even and place all the odd numbers in an array named odd and even numbers in an array named even and output the contents of odd array and even array.
  • Write a function named output. This function will output the odd array to a file named odd.dat and even array to a file named even.dat. This function should be called only if the user wishes to output the data into different file

Example Program Execution

Enter the name of the file to read numbers: numbers.dat
Odd numbers are:
11 21 45 71 81 95 87 71 55 11 17 85 71 61 37 91 13
Even numbers are:
32 52 62 92 10 66 54 34 22 52 46 28 18
Do you want to output the results to even.dat and odd.dat files y/n: y
Example Files
even.dat odd.dat
32 52 62 92 10 66 54 34 22 52 46 28 18
11 21 45 71 81 95 87 71 55 11 17 85 71 61 37 91 13

Lab Submission

  • array_paramater.cpp
  • even_odd.cpp
  • Your tarball and the directory inside it should be named like Smith-1234567-Lab-08
  • The email subject should be "[EECS 168] Lab 08" (without the quotes).

Remember to include your full name, student number, and disk usage in the email.