CSC126

Arrays

 

1)     Write the declaration of an array called temperatures that is of type double and holds 30 values.

 

 

a)      How many bytes does this array take up in memory?

 

 

b)      How can we initialize this array so that all the temperatures start off at 32 degrees?


 

 

2)     What is printed by the following program segment?

 

 

 

 int num[10];

 int i;

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

      num[i] = i - 1;

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

      if (i % 3)

           num[i] = num[i + 1];

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

      cout<<num[i]<<endl;

 

 

 

3)        

#include<iostream>

#include<fstream>

using namespace std;

 

//************************************

//Programmer:

//Date:

//

//program to demonstrate arrays

//************************************

void main()

{

      int j;

      int arr1[10];

      int arr2[10];

      for (j = 0; j < 10; j++)

      {

            arr1[j] = j * 5;

            arr2[j] = arr1[j] + 3;

      }

      for (j = 0; j < 10; j++)

      {

            cout<<arr1[j]<<"  "<<arr2[j]<<endl;

      }

 

}

 

 

4)        

#include<iostream>

#include<fstream>

using namespace std;

/****************************************

//Programmer:

//Date:

//Description:

//***************************************

 

void findpos(int[],int, int &);

const int NUMSPOTS=30;

void main()

{

  int i,num,spot;

  int array[NUMSPOTS];

 

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

        array[i] = i*2;

  cout<<"Enter the number you wish to find";

  cin>>num;

  findpos(array,num,spot);

 

  cout<<"The number that you wish to find is in the";

  cout<<spot<<"spot\n";

}

void findpos(int x[], int num, int & s)

//**************************************

//Programmer:

//Date:

//Finding num in the array x

//***************************************

{

      int i;

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

      {

            if (x[i] == num)

            {

                  s = i;

                  break;     

            }

            cout<<"in the "<<i<<"th iteration\n";

}

      if (i == 30) s = -1;

}