The document discusses one-dimensional arrays in C++. It defines an array as a series of elements of the same type that can be referenced collectively by a common name. These elements are placed in consecutive memory locations and can be individually referenced using a subscript or index. The document covers declaring and initializing one-dimensional arrays, accessing array elements individually and collectively, inputting and displaying array elements, and provides examples of programs that work with arrays.
Introduction of arrays, Declaration of array, Initialization of array, Sorting, Multidimensional array. Some code examples that will make you clear about the concept of arrays.
https://github.com/ashim888/csit-c
Introduction of arrays, Declaration of array, Initialization of array, Sorting, Multidimensional array. Some code examples that will make you clear about the concept of arrays.
https://github.com/ashim888/csit-c
● Concept of Structure and Union
● Declaration and Initialization of structure and union
● Nested structures
● Array of Structures
● Passing structure to functions
While doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Array
Introduction
One-dimensional array
Multidimensional array
Advantage of Array
Write a C program using arrays that produces the multiplication of two matrices.
● Concept of Structure and Union
● Declaration and Initialization of structure and union
● Nested structures
● Array of Structures
● Passing structure to functions
While doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Array
Introduction
One-dimensional array
Multidimensional array
Advantage of Array
Write a C program using arrays that produces the multiplication of two matrices.
An array is a group of data items of same data type that share a common name. Ordinary variables are capable of holding only one value at a time. If we want to store more than one value at a time in a single variable, we use arrays.
An array is a collective name given to a group of similar variables. Each member in the group is referred to by its position in the group.
Arrays are alloted the memory in a strictly contiguous fashion. The simplest array is a one-dimensional array which is a list of variables of same data type. An array of one-dimensional arrays is called a two-dimensional array.
At the end of this lecture students should be able to;
Describe the C arrays.
Practice the declaration, initialization and access linear arrays.
Practice the declaration, initialization and access two dimensional arrays.
Apply taught concepts for writing programs.
This is a ppt created for students studying in class xi cbsewho have opted for artificial intelligence course. it shows the facts about machine learning, its examples, how it works and its uses in daily life.
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
Introduction to looping, for loop. while loop, do loop jump statements, entry controlled vs exit controlled loop, algorithm and flowchart of loops, factorial of a number
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
Andreas Schleicher presents at the OECD webinar ‘Digital devices in schools: detrimental distraction or secret to success?’ on 27 May 2024. The presentation was based on findings from PISA 2022 results and the webinar helped launch the PISA in Focus ‘Managing screen time: How to protect and equip students against distraction’ https://www.oecd-ilibrary.org/education/managing-screen-time_7c225af4-en and the OECD Education Policy Perspective ‘Students, digital devices and success’ can be found here - https://oe.cd/il/5yV
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
How to Split Bills in the Odoo 17 POS ModuleCeline George
Bills have a main role in point of sale procedure. It will help to track sales, handling payments and giving receipts to customers. Bill splitting also has an important role in POS. For example, If some friends come together for dinner and if they want to divide the bill then it is possible by POS bill splitting. This slide will show how to split bills in odoo 17 POS.
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Model Attribute Check Company Auto PropertyCeline George
In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
Palestine last event orientationfvgnh .pptxRaedMohamed3
An EFL lesson about the current events in Palestine. It is intended to be for intermediate students who wish to increase their listening skills through a short lesson in power point.
Ethnobotany and Ethnopharmacology:
Ethnobotany in herbal drug evaluation,
Impact of Ethnobotany in traditional medicine,
New development in herbals,
Bio-prospecting tools for drug discovery,
Role of Ethnopharmacology in drug evaluation,
Reverse Pharmacology.
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
What is the purpose of the Sabbath Law in the Torah. It is interesting to compare how the context of the law shifts from Exodus to Deuteronomy. Who gets to rest, and why?
2. Contents
Arrays
Types of Arrays
One-Dimensional Array
Declaring One-Dimensional Array
Initializing One-Dimensional Array
Accessing elements of One-Dimensional Array
Inputting elements of One-Dimensional Array
Displaying elements of One-Dimensional Array
Example programs
3. Need for array
Imagine you have to write a program to find the sum of marks of
15 students in a class.
For this we would have to declare 15 variables of the same type.
Further to enforce uniformity, we could take the names of the
variables as marks1, marks2, marks3….etc.
The program would look like this:
4. Need for array
#include<iostream.h>
void main()
{ int marks1, marks2, marks3, marks4, marks5, marks6, sum;
cout<<“n enter the marks of 6 students”;
cin>>marks1>>marks2>>marks3>>marks4>>marks5>>marks6;
sum= marks1+marks2+marks3+marks4+marks5+marks6;
cout<<“average marks are “<<sum/6;
}
In the above program, we have declared 6 variables to store marks of each of the 6 students and one
variable to accumulate the sum of marks. To declare and maintain such a large number of similar
variables makes the program bulky and inefficient. To avoid such a situation, we can use arrays.
5. What is an array?
An array is a series of elements (variables) of the same type which
can be referenced collectively by a common name.
These elements are placed in consecutive memory locations .
They can be individually referenced by adding an index or
subscript to the unique name.
In the above scenario, to store marks of the 6 students, we could
declare an array:
int marks[6];
6. Declaration of marks array and memory
representation
int marks[6]; Memory Representation
array name subscript or index value
array element
Name of the array
Number of elements
Data type of
the array
marks[5]
marks[3]
marks[2]
marks[1]
marks[0]
marks[4]
7. Types of arrays
There are basically two types of
arrays depending on the dimension
One dimensional arrays are used to
store lists of data items like marks of
20 students, age of 15 people in a
family etc.
Two dimensional arrays are used to
store list of lists, for example to store
the marks of 20 students in 6 subjects
Types of
Arrays
One
Dimensional
Two
Dimensional
8. Declaring a one-dimensional array
Syntax :
type name[integer constant/literal];
• type is the data type of the array. It can be int, float or char or
user defined(discussed in later chapters)
• name is any valid c++ identifier
• the number of elements in the array has to be an integer
constant or literal.
• The size of the array in bytes= data type size X no of elements
The elements of an array are the individual variables which can be
referenced by a subscript value
9. One dimensional array declarations
int runs[6];
Size of array=2X6=12 bytes
(size of int X no of elements)
Data type of
each element
Name of
the array
const int x=8;
int scores[x];
Here x is a named
constant with value 8.
Thus number of
elements in the above
array=8
float salary[20];
Size of array=4X20=80 bytes
(size of floatX no of elements)
Data type of
each element
No of elementsNo of elements
No of
elements
10. One dimensional arrays examples
One dimensional arrays are simple lists. They may be lists of
numbers, marks or characters depending on the data type of the
array. Examples:
int age[20]; //represents the age of 20 people
float salary[15];// represents the salary of 15 employees
char alphabets[24];// represents the alphabets
11. Memory representation of an array
Consider the following array declaration:
int a[4]; memory representation
Data type =integer
Name=a
Number of elements=4
Size = 4 X 2=8 bytes array elements
Here a is an array having 4 elements , each of type int. Each of the element can
be referred individually using name of the array and subscript value eg, a[0],
a[1] etc.
a[0] a[2] a[3]a[1]
Note: the array subscript always starts from 0. The numbering of subscript of an
array with n elements varies from 0 to n-1. Thus an array of 4 elements will be
having subscripts from 0 to 3
12. Initializing an array
To initialize a one dimensional array we need to specify data values enclosed in
curly braces. These data values are called initializers.
Data-type name[size]={initialvalue1,initialvalue2, initialvalue3,initialvalue4…..};
The data type of the initializers should match with the data type of the array
and number of initializers should match with the size of the array.
If the number of initializers are more than the size of the array, an error is
generated.
15. Accessing array elements
In one dimensional array, every element has one index number
The index number always starts from 0.
For an array of n elements the index varies from 0 to n-1
Thus the index of the element is always one less than the element
number. For example the index value of the 6th element will be 5 and
the index of 1st item is 0.
We can access any element using the array name along with the
element index value inside the square brackets.
We can display, input or perform any other action with the elements just
like simple c++ variables
16. Accessing array elements
The elements of an array can be accessed individually as well as
collectively.
To access individual array elements the syntax is :
name[subscript]
• Example :
For the array marks :
cout<< marks[1]; // displays 24
cout<<marks[4]; // displays 22
15
24
20
18
22
25
marks[0]
marks[1]
marks[2]
marks[3]
marks[4]
marks[5]
17. Accessing array elements individually
Consider an array : int num[4];
Let us assign some values to the
elements individually:
• Example :
num[0]=9;
num[2]=6;
9
6
0
1
2
3
Let us now do some more calculations
with elements of the same array :
• Example :
num[0]=num[0]+2 ;
(num[0]=9, num[0]+2=11)
num[3]=num[2]+num[0];
(num[2]=6, num[0]=11)
11
6
17
18. Some more examples of accessing array
elements
Example Statement Meaning
age[3]=22; Assign the value 22 to the 2nd element of the array age
runs[4]=20*2; Assign the product of 20 and 2 to the 5th element of the array
runs
cin>>list[5] ; Input the 6th element of the array list
scores[7]=scores[4]+90; Add 90 to the 5th element of array score and assign the result to
the 8th element of the array
cout<<age[8 ]; Display the content of the 9th element of array age.
average=(scores[0]+scores[1])/2; find the average of score 1 and 2 and assign it to the variable
average.
age[i-2]= s+t; Assign the sum of s and t to the element of array whose index is
given by the expression i-2
19. Accessing array elements collectively
To assign values collectively we use a loop
Let us assign some values to the elements of the same array
collectively:
• Example : num[0]:= i=0, i*2=0
for(int i=0;i<4; i++) num[1]:= i=1, i*2=2
num[i]=i*2; num[2]:= i=2, i*2=4
num[3]:= i=3, i*2=6
Thus using a loop we can access all the elements of the array one
by one(or sequentially). The value of the index varies from 0 to 3
thus assigning the values to the elements accordingly.
0
2
4
6
20. Input into an array
To input values into an array we can use the for loop. Each element is accessed
individually.
int nums[4];
for(int i=0; i<4; i++)
cin>>nums[i];
The value of i will vary from 0 to 3, thus in the first pass, value of i=0.
cin>>nums[i]= cin>>nums[0]
In the second pass, value of i=1, thus
cin>>nums[i]= cin>>nums[1] and so on.
21. Displaying contents of an array
To diplay values of an array we can use the for loop again to access each
element individually.
for( i=0; i<4; i++)
cout<<nums[i];
The value of i will vary from 0 to 3, thus in the first pass, value of i=0.
cout<<nums[i]= cout<<nums[0]
In the second pass, value of i=1, thus
cout<<nums[i]= cout<<nums[1]
and so on.
22. Program to input values into an array and
display array contents
#include<iostream.h>
void main()
{
int nums[4];
cout<<“Enter the list of numbers :”;
for(int i=0; i<4; i++)
cin>>nums[i];
cout<<“n The numbers you entered are :”;
for (i=0;i<4;i++)
cout<<nums[i]<<“n”;
}
Enter the list of numbers :
2
5
6
8
The numbers you entered are :
2
5
6
8
Output
23. Program to input the marks of 6 students and
find the average score
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float marks[6], sum=0;
for(int i=0; i<6; i++) //loop for reading marks
{
cout<< “Enter marks of student “<< i+1<<”:”;
cin>> marks[i];
}
for(int j=0; j<6; j++) //loop for accumulating sum of marks
{
sum=sum+marks[j];
}
cout<<endl<<”Marks you have entered are :”<<endl;
for(j=0; j<6; j++) //loop for displaying the marks
{
cout<< “Marks of student “<< j+1<<”:”;
cout<< a[j]<<endl;
}
cout<<”nTotal marks : “<<sum;
cout<<“nAverage marks :”<<sum/6;
getch();
}
Enter marks of student 1: 24
Enter marks of student 2: 25
Enter marks of student 3: 22
Enter marks of student 4: 20
Enter marks of student 5: 25
Enter marks of student 6: 22
Marks you have enetered are :
Marks of student 1:24
Marks of student 2:25
Marks of student 3:22
Marks of student 4:20
Marks of student 5:25
Marks of student 6:22
Total marks :138
Average marks :23
Output
24. Entering unknown number of elements
The memory is allocated to an array at compile time. So this makes
it compulsory to declare an array size at the time of declaration.
But at times we do not need to input or fill each and every array
element. For example, in a school we have to define an array
marks to store data of 50 students because maximum number of
students in any class in the school is 50. But there will be classes
having lesser number of students.
25. Entering unknown number of elements…contd
For this we need to partially
fill the array depending on
the number of students eg 25
or 40 etc. So we take a
variable say n and run the
loop from 0 to n-1.
int marks[50], n;
cout<<“nenter number of students:
“;
cin>>n;
for(int i=0;i<n;i++)
cin>>marks[i];
Here marks[50] is the array and n is
the number of students whose marks
are to be entered. So the loop will run
from 0 to n-1 and input marks of n
students.
Explanation
Example
26. #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float marks[50], sum=0,n;
cout<<“Enter number of students in class:”;
cin>>n;
for(int i=0; i<n; i++) //loop for reading marks
{
cout<< “Enter marks of student “<< i+1<<”:”;
cin>> marks[i];
}
for(int j=0; j<n; j++) //loop for accumulating sum of marks
{
sum=sum+marks[j];
}
cout<<”nTotal marks : “<<sum;
cout<<“nAverage marks :”<<sum/n;
getch();
}
Program to input the marks of n students and
find the average score
Enter number of students in class
4
Enter marks of student 1: 90
Enter marks of student 2: 80
Enter marks of student 3: 75
Enter marks of student 4: 85
Total marks : 330
Average marks :82.5
Output
27. Program to read temperatures for unknown
number of days and find average temperature
#include<iostream.h>
void main()
{
float temp[20]; int n=0;char ch=‘y’;
do
{cout<<“nEnter temperature for day ”<<n+1<<“:”;
cin>>temp[n];
cout<<“nEnter more (y/n)? ”;
cin>>ch;
n++;
}while(ch!=‘n’ || ch!=‘n’);
for(int i=0; i<n;i++)
sum+=temp[i];
cout<<“nAverage temp is :”<<sum/n;
}
Enter temperature for day 1: 25.5
Enter more(y/n)? Y
Enter temperature for day 2: 26.5
Enter more(y/n)? Y
Enter temperature for day 3: 27.0
Enter more(y/n)? Y
Enter temperature for day 4: 24.5
Enter more(y/n)? Y
Enter temperature for day 5: 25.0
Enter more(y/n)? N
Average temp is : 25.7
Output
28. Array out of bounds
Consider the array marks declared in the above program to store
the marks of 6 students. Although we have input marks of 6
students using the loop. But what if a user enters more than 6
marks. Lets say marks of 20 students?
We would expect an error. But this does NOT happen. The data
thus entered will be placed in locations outside the array , maybe
in other programs’ memory.
This happens because, in c++ there is no way to check if the
number of elements entered into an array exceeds the size or
number of elements declared in the array.
This may cause some unexpected program behavior and may
cause errors.
29. Program to find maximum number in an array
#include<iostream.h>
void main()
{
int nums[20];
cout<<Ënter the number of list items:”;
cin>>n;
cout<<“Enter the list of numbers :”;
for(int i=0; i<n; i++)
cin>>nums[i];
int max=nums[0];
for (i=0;i<n;i++)
if(nums[i]>max)
max=nums[i];
cout<<“n The largest number is :”<<max;
}
Enter the number of list items:
6
Enter the list of numbers :
22
11
56
62
80
44
The largest number is : 80
Output