Arrays

 

Problem:  The weather service reports its 9AM temperature readings each day and prints these temperatures in a well organized report.  The report is organized such that these temperatures are printed in a column across the page with the  equivalent Celsius temperature printed underneath.  Write the computer program that reads 5 temperatures from a data file and prints this report.

 

#include <iostream>

#include <fstream>

#include <iomanip>

 

using namespace std;

 

int

main()

{

      float temp1, temp2, temp3, temp4, temp5, temp6, temp7;

      ifstream fin;

     

      fin.open("input.txt");

      if (fin.fail())

      {

            cerr << "File failed to open";

            abort();

      }

 

      fin >> temp1 >> temp2 >> temp3 >> temp4 >> temp5 >> temp6 >> temp7;

 

      cout << setw(18) << "Sunday"

            << setw(10) << "Monday"

            << setw(10) << "Tuesday"

            << setw(10) << "Wednesday"

            << setw(10) << "Thursday"

            << setw(10) << "Friday"

            << setw(10) << "Saturday";

      cout << endl;

 

      cout << setw(8) << "temp F";

      cout << setw(10) << temp1

            << setw(10) << temp2

            << setw(10) << temp3

            << setw(10) << temp4

            << setw(10) << temp5

            << setw(10) << temp6

            << setw(10) << temp7;

      cout << endl;

 

      cout << setw(8)<< "temp C";

      cout << fixed << setprecision(0);

      cout << setw(10) << 5/9.0*(temp1 -32)

            << setw(10) << 5/9.0*(temp2 -32)

            << setw(10) << 5/9.0*(temp3 -32)

            << setw(10) << 5/9.0*(temp4 -32)

            << setw(10) << 5/9.0*(temp5 -32)

            << setw(10) << 5/9.0*(temp6 -32)

            << setw(10) << 5/9.0*(temp7 -32);

      cout << endl;

 

 

      return 0;

}

 

            Sunday    Monday   Tuesday Wednesday  Thursday    Friday  Saturday

  temp F        23        32        56        81        66        52        78

  temp C        -5         0        13        27        19        11        26

Press any key to continue

 

What if we processed 100 temperatures?

 

Array – Structured data type consisting of a group of elements.  Elements of an array are contiguous memory locations.  Each element of an array is of the same data type.  An array allows us to associate one identifier with many elements. This is done by a subscript known as an index.

 

temp[0]

 

temp[1]

 

temp[2]

 

temp[3]

 

temp[4]

 

temp[5]

 

temp[6]

 

 

Each cell of an array is called an element of that array.

Each element is referenced by the identifier and its subscript.

 

Ex. temp[3] = 70;

 

The array index or subscript will range from 0 to one less than the size of the array.

 


 

How to declare an array

 

int temp[7];

 

datatype  arrayidentifier [size]

where datatype is the type of the array, array identifier is a valid C++ identifier, and size is an integer or integer expression.

 

const int size = 6;

float high[size];

 

int i, j, low[10];

 

Just as when we initialize a single variable in the declaration statement, we can do the same with an array.

 

int num = 5;

int temp[7] = { 23, 32, 56, 81, 66, 52, 78};

 

 

temp[0]

23

temp[1]

32

temp[2]

56

temp[3]

81

temp[4]

66

temp[5]

52

temp[6]

78

 

int junk = { 4, 5, 8, 75, 45};

 

junk[0]

4

junk[1]

5

junk[2]

8

junk[3]

75

junk[4]

45

 

The compiler know that the array has 5 elements from the initialization.

 

We can access an array element through its subscript.

 

 cout << junk[2] << ‘ ‘ << junk[4];  //prints 8 45

i = 2;

cout << junk[i + 2];  // prints 45

 

How can we print each element of junk ?

 

for (i = 0; i < 5; i++)

     cout << junk[i] << ' ';

 

Temperature program with arrays

#include <iostream>

#include <fstream>

#include <iomanip>

 

using namespace std;

 

int

main()

{

      float temp[7];

      int i;

      ifstream fin;

 

      fin.open("input.txt");

      if (fin.fail())

      {

            cerr << "File failed to open";

            abort();

      }

 

      for (i = 0; i < 7; i++)

            fin >> temp[i];

 

      cout << setw(18) << "Sunday"

            << setw(10) << "Monday"

            << setw(10) << "Tuesday"

            << setw(10) << "Wednesday"

            << setw(10) << "Thursday"

            << setw(10) << "Friday"

            << setw(10) << "Saturday";

      cout << endl;

      cout << setw(8) << "temp F";

 

      for (i = 0; i < 7; i++)

                  cout << setw(10) << temp[i];

      cout << endl;

 

      cout << setw(8)<< "temp C";

      cout << fixed << setprecision(0);

 

      for (i = 0; i < 7; i++)

                  cout << setw(10) << 5/9.0*(temp[i] -32);

      cout << endl;

      return 0;

}

 

Problem:  Write a C++ program which will read integer values into a 5 element array.  It then takes each element and shifts them one position forward making the last element of the “old” array the first element in the “new” array.  Use only one array.

Ex. 

 

5

 

17

8

 

5

12

 

8

23

 

12

17

 

23

 

#include <iostream>

#include <fstream>

#include <iomanip>

 

using namespace std;

 

int

main()

{

      int shifty[5] = {5, 8, 12, 23, 17};

 

      int i, temp;

 

      for (i = 0; i < 5; i++)

            cout << shifty[i] << ' ';

      cout << endl;

 

      temp = shifty[4];

      for (i = 3; i >= 0; i--)

            shifty[i+1] = shifty[i];

 

      shifty[0] = temp;

 

      for (i = 0; i < 5; i++)

            cout << shifty[i] << ' ';

      cout << endl;

 

 

      return 0;

}

 

Problem:  Read integer numbers into a 5 element array.  Print the array.  Divide each element by 3 and store the number in a second array of equal size but of type float.  The answers to 3 decimal places.

 

#include <iostream>

#include <fstream>

#include <iomanip>

 

using namespace std;

 

int

main()

{

      int first[5] = {5, 8, 12, 23, 17};

      float second[5];

 

      int i;

 

      cout << "The first array is: ";

      for (i = 0; i < 5; i++)

      {

            cout << first[i] << ' ';

            second[i] = first[i]/3.0;

      }

      cout << endl;

     

      cout << fixed << setprecision(3);

      cout << "The second array is: ";

      for (i = 0; i < 5; i++)

            cout << second[i] << ' ';

      cout << endl;

 

 

      return 0;

}

 

Parallel arrays are arrays of equal size and different data types that have some relationship to one another.