0% found this document useful (0 votes)
13 views28 pages

Chapter 7 - 1D Arrays

Chapter 7 discusses arrays, which are collections of fixed-size elements of the same data type, and explains how to declare, initialize, and manipulate them in programming. It covers one-dimensional and two-dimensional arrays, array initialization, inputting values, and using arrays as parameters in functions. Additionally, it includes programming challenges related to arrays, such as calculating averages and counting occurrences.

Uploaded by

yobh05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views28 pages

Chapter 7 - 1D Arrays

Chapter 7 discusses arrays, which are collections of fixed-size elements of the same data type, and explains how to declare, initialize, and manipulate them in programming. It covers one-dimensional and two-dimensional arrays, array initialization, inputting values, and using arrays as parameters in functions. Additionally, it includes programming challenges related to arrays, such as calculating averages and counting occurrences.

Uploaded by

yobh05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter 7

Arrays
Challenge problem
Write a program that read
marks for 5 test , calculate
average and then count
how may test above
average
Arrays
• An array is a collection of a fixed number of
elements (or “components”), all the same
data type.
• For example, an array might contain:
• Five elements, each of which is an int.
• One hundred elements, each of which is a
double.
• Seven thousand elements, each of which is a
char.
Array Dimension
Every array has a dimension.
• one-dimensional array
0 1 2 3 4
test

• two-dimensional array
0 1 2 3 4
0
test 1
2

C++ Programming: From Problem Analysis to Program


4
Design, Seventh Edition
Arrays View
Different variables declarations:

test1 test2 test3 test4 test5

Array declarations:
More accurate

0 1 2 3 4
test
How to declare One Dimension Array
datatype [ ] array_name; OR datatype array_name [ ];

int myList [ ] = new int [10];

int [ ] myList = new int [10];

Examples:
int test[]= new int [5];
double myArray1[]= new double [100];
char myArray2[];
myArray2 = new char [7000];
int list[ ] = new int [10];
Array Initialization During Declaration
• You can initialize an array when you declare it by listing values between curly braces.

– If no array size is given inside the square brackets, the array’s size is equal to the number of
initial values in the braces.

• Example:

double sales[] = {12.25, 32.50, 16.90, 45.68};

0 1 2 3
sales 12.25 32.50 16.90 45.68
Array Initialization or inputting values from the user
Let consider
int list[] =new int [4];
0 1 2 3

How can we initialize all element to 20? list 20 20 20 20

list=20 Error

You should loop (For is most used )


Input Array values from the user
Let consider
int list[] =new int [4];
How can we read all element from the user ?

list= kbd.nextInt(); Error

You should loop (For is most used )


Display array values on screen
Let consider
int list[] ={10,20,30,40,50 };
How can we print it on screen ?

System.out.print(list) Error
You should loop (For is most used )
Write a program that do
the following:
• Declare an array to store 10 integers.
• Ask the user to enter 10 integers and
store them inside array .
• Ask the user to enter an integer x.
• Count how many x repeated in the
array.
Write a program that do
the following:
• Declare an array to store temperature
of a week.
• Ask the user to enter 7 temperature
of the week and store them inside
array .
• Calculate the average temperature
for a week
• Count how days are above average
Write a program that do
the following:
• Declare an array to store temperature
of a week.
• Ask the user to enter 7 temperature
of the week and store them inside
array .
• Calculate the average temperature
for a week
• Count how days are above average
Arrays as Parameters to Functions

• you can pass arrays to the functions as parameter


and also you can return it.
• When used as function parameters, arrays are
always passed by reference Which mean if you
change anything in the array it will be change in the
main source too.
What is the output of the following code?
Passing Arrays to function
Write a function called
PrintOdd that accept array of
integers as parameters.

The function print all the


values that are odd and more
than the average.
Continue. .
Write a main function that
define an integer array of size
40. the program asks the user
to fill the array from the
keyboard.

After filling the array, the


program should call the
function PrintOdd.
Passing Arrays to function

Write a function called addFive that


accept array of integers as
parameters. The function add 5 to all
the values in the array.

Write a program that define an

integer array of size 10 and fill the

array from the keyboard.

Call the method addFive and print

the array after the call.


Passing Arrays to function

Write a function called


findMax that accept array of
marks as parameters.

The function will return the


maximum value in the list
Continue. . .
Write a program that read
temperature for 30 days and
store them inside array.

Then find the maximum


temperature by calling the
findMax method.
Parallel Arrays
Parallel arrays
• multiple arrays of the same size such that i-th element of each
array is related to all i-th elements in the other arrays.

Example name 0 1 2
ALI NOORA MARIAM
height 0 1 2
169 158 201

weight 0 1 2
65.5 67.1 91.3

You might also like