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

Arrays: Structured Programming

Fahad Majeed discusses arrays and structured programming. Arrays allow storing multiple values in a single variable and accessing them through indices. This is more efficient than declaring multiple variables when dealing with large amounts of data. Majeed explains arrays can solve the problem of rewriting a program to read 100 numbers instead of just 5 without increasing the number of variables declared. An example program demonstrates using arrays to find the sum and average of 10 sales values entered by the user.

Uploaded by

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

Arrays: Structured Programming

Fahad Majeed discusses arrays and structured programming. Arrays allow storing multiple values in a single variable and accessing them through indices. This is more efficient than declaring multiple variables when dealing with large amounts of data. Majeed explains arrays can solve the problem of rewriting a program to read 100 numbers instead of just 5 without increasing the number of variables declared. An example program demonstrates using arrays to find the sum and average of 10 sales values entered by the user.

Uploaded by

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

Arrays

Structured Programming

By
Fahad Majeed
Research Associate
University of Engineering & Technology, Taxila
Its your turn first!!!


Lab Journal (Repetition Control Structures)


Recall
 Before formally defining an array, let us consider
the following problem.

 We want to write a C++ program that reads five


numbers, finds their sum, and prints the numbers
in reverse order.
Recall
What’s wrong with this Program?

 This program works fine.

 However, if you need to read 100 (or more) numbers and print them
in reverse order, you would have to declare 100 variables and write
many cin and cout statements.

 Thus, for large amounts of data, this type of program is not


desirable.
What is an Array?
Lets Watch this video!
Array program to find sum & Average
#include "stdafx.h"
for (index = 0; index < 10; index++)
#include <iostream>
{
using namespace std; cout<<"Entered value for
int main() sales["<<index<<"]"<<
{ sales[index]<<endl;
double sales[10];
}
int index;
//Finding the sum and average of an array:
double largestSale, sum, average; sum = 0;
for (index = 0; index < 10; index++)
for (index = 0; index < 10; index++) sum = sum + sales[index];
{ average = sum / 10;
cout<<"please enter the sale value for cout<<"The sum and average of an
sales["<<index<<"]"; array"<<average<<endl;
cin>> sales[index] ;
system("pause");
}
return 0;
}
Example
Now that we know how to declare
and process arrays, let us rewrite
the program that we discussed in
the beginning of this Lecture .

Recall that this program reads five


numbers, finds the sum, and prints
the numbers in reverse order.

You might also like