Lab No 06
One Dimensional Arrays
Objectives
Objectives of this lab are:
• Understand the use of arrays in C++ Language
• Understand the declaration of one-dimensional arrays
Overview
In programming, array is a type of data structure that is used to store elements of the same data
type in continuous memory locations. Arrays let us store multiple values of the same data type
in a single variable. Different types of arrays make it possible to store the values in various
structures. One of the most common structures of an Array in which the values are stored is the
One Dimensional Array Structure.
Definition
1. One-Dimensional Array is the simplest form of an Array in which the elements are
stored linearly and can be accessed individually by specifying the index value of each
element stored in the array.
2. A One-Dimensional Array is a group of elements having the same data type which are
stored in a linear arrangement under a single variable name.
One Dimensional Array
This is one of the simplest forms of Array. They are very easy to define and use in the programs.
The values stored in a One Dimensional Array can be easily initialized and manipulated,
making it a very feasible Data structure type.
Declaration Syntax
data_type array_name [array_size] ;
where,
array_name = name of the 1D array
array_size = defines the number of elements in the array
Department of Electrical and Computer Engineering 71
Initialization Syntax
To Initialize the 1D Array, we simply add a list to the right side of the declaration syntax of the
1D Array. In simple terms, we assign values to the declared 1D Array as per the array size
specified.
data_type array_name [array_size]= {comma_separated_element_list};
Note: The number of elements specified in the list should not exceed the defined Array Size.
Input Elements in 1D Array
There are few methods by which we can assign and store values in an Array. Let us understand
by looking at the 2 most common methods.
1. Direct Initialization
In this method, the elements are assigned during the declaration of the 1D Array.
Example
int num [10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10} ;
2. User Input Method
In this method, The user is asked to enter the array elements of his/her choice during the run-
time of the program.
Example
#include <iostream>
using namespace std;
int main()
{
int num [5] ;
cout<<"Enter array elements : \n";
for(int i = 0; i < 5 ; i++)
{
cin >> num[i] ;
}
}
Department of Electrical and Computer Engineering 72
Accessing 1D Array Elements
We can either display the entire 1D Array to know what the elements are or we can use the
method of indexing in order to display only those Array values which we require.
Note: Array Indexing always starts from 0.
Example
To print a single element of an array
int arr [5] = {1, 3, 5, 7, 9} ;
cout << arr[3] ; // arr[3] i.e. index 3 of array will print the
value 7
Declaration of Strings in 1D Arrays
One Dimensional Array not only includes numeric data but can also contain alphabetic values.
Strings are nothing but a collection of characters, or we might also say Strings are an array of
characters. Strings can also be stored in Arrays by using the char data type. The syntax for
initializing a string array is as follows:
Syntax
char string_name [string_size] = {comma_seperated_character_list} ;
Example
char str [20] = {"Hello World"} ;
cout << str ;
Using Strings in 1D Array, we can perform various operations such as finding the length of a
string, comparing 2 strings, copy a string, reverse the string, delete specific words/alphabets
from the strings, count the number of words/letters, etc.
Applications of 1D Arrays
One Dimensional Arrays are used to implement other data structures such as stacks, queues,
heaps, graphs, etc. Using 1D Arrays, we can perform operations such as finding the position of
any element in the array, find out the largest and the smallest element in the array, insert and
delete an element, merge two arrays, etc. 1D arrays are also used to implement Sorting
Algorithms such as Insertion Sort, Bubble Sort, Selection Sort, Merge Sort, etc.
Department of Electrical and Computer Engineering 73
Activity No 01
Write a program that gets input from user in one dimensional integer array of size 10. Your
program should find and display maximum value in the array on Screen.
Activity No 02
Write a program that defines a char array of size 50. Program will ask the user to enter a String.
Program should count the number of times vowel characters appear in the string and display
the count on screen
Activity No 03
Write a Program that will ask the user to enter two strings. Program will compare two strings
using Loops and tell if the strings are same or not.
Activity No 04
Write a User defined function that will take an integer array and its size as input. Function will
reverse the elements in the array. In Main(), ask the user to fill an array of size 10. Main() will
call the function to reverse the elements in .the array. Main () will also display the array on
screen, before and after calling the function.
Activity No 05
Write a program that defines an array of size 20 and type float. Program should fill the array
with the terms of series shown below. Program will also display the array on Screen.
1 , 1/22 , 1/32 , 1/42 , … , 1/N2
Department of Electrical and Computer Engineering 74
Lab Exercise and Summary
Summary should cover Introduction, Procedure, Data Analysis and Evaluation.
Department of Electrical and Computer Engineering 75
Student Signature Date
Department of Electrical and Computer Engineering 76
LABORATORY SKILLS ASSESSMENT (Psychomotor)
Total Marks: 100
Criteria Level 1 Level 2 Level 3 Level 4 Score
(Max Marks) 0% ≤ S < 50% 50% ≤ S< 70% 70% ≤ S< 90% 90%≤ S ≤100% (S)
Selects Selects and applies Selects and Selects and applies
inappropriate partially applies the the completely
Procedural skills and/or appropriate skills considerably appropriate
Awareness strategies to and/or strategies appropriate strategies and/or
(20) write and required by the strategies and/or skills specific to the
compile the programs. skills specific to programs.
programs. the programs.
Makes major Makes numerous Makes minor non- Applies the
critical errors in critical errors in critical errors in procedural
applying applying applying knowledge of
procedural procedural procedural datatypes,
Practical
knowledge of knowledge of knowledge of operators, and
Implementation
datatypes, datatypes, datatypes, statements in C++
(20)
operators, and operators, and operators, and programming in
statements in statements in C++ statements in C++ optimized ways.
C++ programming. programming.
programming.
Program logic Program logic has Program logic is Program logic is
has many errors some errors with mostly correct, correct, with no
with majority of several but may contain known errors, and
Program Logic contradictory contradictory occasional errors no redundant
(20) conditions. conditions. or redundant/ or contradictory
contradictory conditions.
conditions.
Program does not Program partially Program adequately Program completely
follow proper follow proper follow proper follow proper syntax
Syntax syntax of C++ syntax of C++ syntax of C++ of C++ programming
Correctness and programming and programming and programming and and does not produce
Results (20) does not produce does not produce does not produce desired results for
desired results for desired results for desired results for most inputs.
most inputs. most inputs. most inputs.
Uses software Uses software Uses software Uses software tool,
Use of Software tool, with limited tool, with some tool, with with a high degree
Tool competence. competence. considerable of competence.
(10) competence.
Requires Requires some Follows safety Routinely follows
constant reminders to procedures with safety procedures.
Safety reminders to follow only minimal
(10) follow safety safety procedures. reminders.
procedures.
Marks Obtained
Instructor Name: Sign:
Department of Electrical and Computer Engineering 77
LABORATORY SKILLS ASSESSMENT (Affective)
Total Marks: 40
Criteria Level 1 Level 2 Level 3 Level 4
0% ≤ S < 50% 50% ≤ S < 70% 70% ≤ S < 90% 90% ≤ S ≤ 100% Score
(Max. Marks)
Introduction Very little Introduction is brief Introduction is nearly Introduction complete
(5) background with some minor complete, missing and well-written;
information mistakes some minor points provides all necessary
provided or background principles
information is for the experiment
incorrect
Procedure Many stages of the Many stages of the The procedure could The procedure is well
(5) procedure are not procedure are be more efficiently designed and all stages
entered on the lab entered on the lab designed but most of the procedure are
report. report. stages of the entered on the lab
procedure are entered report.
on the lab report.
Data Record Data is brief and Data provides some Data is almost Data is complete and
(10) missing significant significant complete relevant. Tables with
pieces of information but has some minor units are provided.
information. and has few critical mistakes. Graphs are labeled.
mistakes. All questions are
answered correctly.
Data Analysis Data is presented Data is presented in Data is presented in Data is presented in
(10) in very unclear ways that are not ways that can be ways that best
manner. clear enough. understood and facilitate
interpreted. understanding and
interpretation.
Report Quality Report contains Report is somewhat Report is well Report is well
(10) many errors. organized with some organized and organized and
spelling or cohesive but contains cohesive and contains
grammatical errors. some grammatical no grammatical errors.
errors. Presentation seems
polished.
Marks Obtained
LABORATORY SKILLS ASSESSMENT (Cognitive)
Total Marks: 10
(If any)
Marks Obtained
Instructor’s Signature: Date:
Department of Electrical and Computer Engineering 78