0% found this document useful (0 votes)
12 views

Week#11 Arrays

The document provides an overview of programming fundamentals related to arrays. It defines an array as a collection of adjacent memory cells that store the same type of data and are referenced by the same name. It discusses defining and initializing arrays, accessing array elements using indices, inputting and outputting array values using for loops, and provides examples of programs that average values in an array, subtract corresponding elements of two arrays and store the results in a third array, and find the minimum and maximum values in an array.

Uploaded by

ramy saad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week#11 Arrays

The document provides an overview of programming fundamentals related to arrays. It defines an array as a collection of adjacent memory cells that store the same type of data and are referenced by the same name. It discusses defining and initializing arrays, accessing array elements using indices, inputting and outputting array values using for loops, and provides examples of programs that average values in an array, subtract corresponding elements of two arrays and store the results in a third array, and find the minimum and maximum values in an array.

Uploaded by

ramy saad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming Fundamentals

Arrays
Lecture Outline
1 What is an array?
2 Defining and accessing Arrays

3 Arrays input

4 Arrays output

5 Example
1. What is an Array?

• Simple data types use a single memory cell to store a single value
of data
• For many problems you need to group data items together
• Array allows a programmer to group such related data items of
the same data type together into a single data structure (Array).

• An array is a collection of two or more adjacent memory cells


that:
–Store the same type of data values (e.g. int)
–Are referenced by the same name (i.e using one variable)
–These individual cells are called array elements
2. Defining an Array
To declare an array, we must declare its name, type of data values it will store
and the number of cells associated with it.

Syntax
Array_Type Array_Name[Length];
2. Defining an Array(initialization)
• We use indices to differentiate between the individual array elements
• If you have all the values at the point of declaring the array, you can
declare and initialize the array at the same time,

• If there are values in the initialization block, but not enough to


fill the array, all the elements in the array without values are
initialized to 0 in the case of double or int, and NULL in the case
of char.
2. Defining an Array(initialization)

• int scores[20] = {0}; //all 20 elements in array score are initialized


to 0

• If there are values in the initialization block, an explicit size for the
array does not need to be specified. Only an empty array element is
sufficient, C++ will count the size of the array for you.

int scores[] = {20, 10, 25, 30, 40}; // size of the


array score is
automatically calculated as 5
Accessing array elements
• point[1] // the 2nd element of array point is
accessed
• point[9] = 20; // the 10th element of array point is
assigned the value 20
• We can use a loop to access all the elements of an
array
Example: Adding the values of all array elements

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


sum += a[i];
3. Arrays Input

In order to input the values from the user and


store it in an array, the for loop is used to save
the values one by one
for ( i = 0; i < 10; i++ )
{ cin>>n[ i ] ; }
4. Arrays Output
In order to display the values of an
array, the for loop is used.

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


{ cout<< n[ i ] ; }
5. Example 1

#include <iostream>
using namespace std;
Write a int main( )
{
program to get const int SIZE = 10;
int a[ SIZE ], i, total = 0;
the average of float avg;
10 integers in for ( i = 0; i < SIZE; i++ )
{
an array. cout<<“\n enter the value”;
cin>>a[i];
total += a[ i ];
}
avg=total/10;
cout<< “Average of 10 elements =“<<avg;
return 0;
}
5. Example 2 #include <iostream>
using namespace std;
int main( void )
Write a program to {
const int SIZE =5;
input data into two int first[SIZE], second[SIZE], diff[SIZE], i;
arrays of size 5 cout<<“Enter”<<SIZE<<“data items for 1st array: ";
for(i=0;i<SIZE; i++) // input first array
elements each and cin>>first[i];
subtract their
cout<<"Enter”<<SIZE<<“data items for 2nd array : ";
corresponding for(i=0;i<SIZE; i++) // input second array
elements, cin>>second[i];
for(i=0;i<SIZE; i++) // compute the differences
storing the result in diff[i]= second[i] - first[i];
another array.
cout<<"\n\nOutput of the arrays : \n";
for(i=0;i<SIZE; i++) // output the arrays
cout<<“\t“<< first[i]<<“\t”<<second[i]<<“\t”<<
diff[i]<<endl ;
return 0;
}
5. Example 3

Write a C++ code that ask the user to enter 10


values and save them in an array using for loop.
Then print the minimum and the maximum of the
values.
5. Example 3
#include<iostream>
using namespace std; max=a[0];
int main() for(i=1;i<size;i++)
{ { if(max<a[i])
const int size =10 max=a[i];
int a[size],i,min,max; }
for(i=0;i<size;i++) cout<<“the maximum number is”<<max;
{ cout<<“enter the number \n”; }
cin>>a[i];
}
min=a[0];
for(i=1;i<size;i++)
{ if(min>a[i])
min=a[i];
}
cout<<“the minimum number is ”<<min<<endl;
Thank You

You might also like