CSC211

Handout #1 Arrays

 

 

  1. What is printed by this code?

 

      int counts[5];

      int i;

 

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

           counts[i] = i - 3;

 

      for (i = 2; i < 4; i++)

           counts[i] = 2;

 

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

           cout << counts[i] << ' ';

 

  1.  
    1. Let’s start simply….Suppose that we want to declare an array to hold 12 floating point numbers:

 

    1. Now that we have our array, set all 12 numbers to -100.

 

 

 

    1. Our array now looks like this:

 

0

-100

1

-100

2

-100

3

-100

4

-100

5

-100

6

-100

7

-100

8

-100

9

-100

10

-100

11

-100

 

Change all even positions (all lines with an even index: 0,2,4, etc) to 96.8

 

 

    1. Use a function call to print the array.

 

 

 

  1. Given an array with 35 integers, print out only those numbers that occur more than once. Explain first, in English, how you would do this.  Then write a program to solve the problem.