SlideShare a Scribd company logo
1 of 14
Lab 9
CS106 - Fundamentals of Computer Science
Presented by TA. Nada Kamel
Agenda
 1D Array
 Hands-on problems
Presented by TA. Nada Kamel
1D Array
Presented by TA. Nada Kamel
What is an array?
An array is a collection of data elements of same data type.
Presented by TA. Nada Kamel
Array Declaration
dataType arrayName[arraySize];
For example,
an array containing 5 integer values of type int called foo could be
represented as:
int foo[5];
Presented by TA. Nada Kamel
Array Initialization
double balance [4] = { 1000.0, 2.0, 3.4, 7.0 };
Presented by TA. Nada Kamel
int bar [5] = { 10, 20, 30 };
int baz [5] = { };
Accessing Array Values
The syntax to access a single element in an array is:
arrayName[index]
Presented by TA. Nada Kamel
Example 2
foo[2] = 7; // Intializing index 2 of array foo with 7
Example 1
x = foo[2]; // Storing the value of foo[2] in variable x
cout << x; // Output is 77
Accessing Array Values (Cont.)
The syntax to access All values of an array is by using a loop.
int foo [] = {16, 2, 77, 40, 70};
int result=0;
for ( int n=0 ; n<5 ; n++ )
{
result += foo[n];
}
cout << result << endl;
Presented by TA. Nada Kamel
If no value in the square brackets, you
have to initialize the array to have a size in
the memory.
// result = 16+2+77+40+70 = 205
Hands-on Problems
Presented by TA. Nada Kamel
Problem 1
Write a C++ program that takes a positive integer value for n (not more than
55) and computes the sum of the following series:
1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + ...... + n
Presented by TA. Nada Kamel
int n;
cin >> n;
if (n < 1 || n > 55)
cout << “Please enter a positive number.n”;
else
{
int values[55], sum = 0;
for (int j = 0; j < n; j++)
{
if(j == 0 || j == 1)
values[j] = 1;
else
values[j] = values[j-2] + values[j-1];
sum += values[j];
}
cout << “Sum of value(s) is ” << sum << endl;
}
Problem 2
Trace the following code
int j;
int one[5], two[10];
for (j = 0; j < 5; j++)
one[j] = 5 * j + 3;
cout << “One contains: ”;
for (j = 0; j < 5; j++)
cout << setw(5) << one[j];
cout << endl;
for (j = 0; j < 5; j++)
{
two[j] = 2*one[j] - 1;
two[j + 5] = one[4 - j] – 1 + two [j];
}
cout << “Two contains: ”;
for (j = 0; j < 10; j++)
cout << setw(5) << two[j];
cout << endl;
Output
One contains: 3 8 13 18 23
Two contains: 5 15 25 35 45 27 32 37 42 47
Presented by TA. Nada Kamel
Any Questions?
Presented by TA. Nada Kamel

More Related Content

What's hot

Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
ecko_disasterz
 

What's hot (20)

Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
20BCE1734.pdf
20BCE1734.pdf20BCE1734.pdf
20BCE1734.pdf
 
Functions in c++,
Functions in c++,Functions in c++,
Functions in c++,
 
Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
 
TDD - Test Driven Development in Swift (iOS) with Cuckoo, Quick and Nimble
TDD - Test Driven Development in Swift (iOS) with Cuckoo, Quick and NimbleTDD - Test Driven Development in Swift (iOS) with Cuckoo, Quick and Nimble
TDD - Test Driven Development in Swift (iOS) with Cuckoo, Quick and Nimble
 
Functional programming java
Functional programming javaFunctional programming java
Functional programming java
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
Team 6
Team 6Team 6
Team 6
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
BB - Functions (Operations and Piecewise)
BB  - Functions (Operations and Piecewise)BB  - Functions (Operations and Piecewise)
BB - Functions (Operations and Piecewise)
 
MCM FUNCTIONS BLACKBOARD COMPATIBILITY
MCM FUNCTIONS BLACKBOARD COMPATIBILITYMCM FUNCTIONS BLACKBOARD COMPATIBILITY
MCM FUNCTIONS BLACKBOARD COMPATIBILITY
 
Lab 1
Lab 1Lab 1
Lab 1
 
Exp 3-2 d422 (1)
Exp 3-2  d422 (1)Exp 3-2  d422 (1)
Exp 3-2 d422 (1)
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
GC in C++0x [eng]
GC in C++0x [eng]GC in C++0x [eng]
GC in C++0x [eng]
 

Similar to CS106 Lab 9 - 1D array

2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 

Similar to CS106 Lab 9 - 1D array (20)

Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
White box-sol
White box-solWhite box-sol
White box-sol
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
Array
ArrayArray
Array
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays
ArraysArrays
Arrays
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Arrays
ArraysArrays
Arrays
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 

Recently uploaded

Recently uploaded (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

CS106 Lab 9 - 1D array

  • 1. Lab 9 CS106 - Fundamentals of Computer Science Presented by TA. Nada Kamel
  • 2. Agenda  1D Array  Hands-on problems Presented by TA. Nada Kamel
  • 3. 1D Array Presented by TA. Nada Kamel
  • 4. What is an array? An array is a collection of data elements of same data type. Presented by TA. Nada Kamel
  • 5. Array Declaration dataType arrayName[arraySize]; For example, an array containing 5 integer values of type int called foo could be represented as: int foo[5]; Presented by TA. Nada Kamel
  • 6. Array Initialization double balance [4] = { 1000.0, 2.0, 3.4, 7.0 }; Presented by TA. Nada Kamel int bar [5] = { 10, 20, 30 }; int baz [5] = { };
  • 7. Accessing Array Values The syntax to access a single element in an array is: arrayName[index] Presented by TA. Nada Kamel Example 2 foo[2] = 7; // Intializing index 2 of array foo with 7 Example 1 x = foo[2]; // Storing the value of foo[2] in variable x cout << x; // Output is 77
  • 8. Accessing Array Values (Cont.) The syntax to access All values of an array is by using a loop. int foo [] = {16, 2, 77, 40, 70}; int result=0; for ( int n=0 ; n<5 ; n++ ) { result += foo[n]; } cout << result << endl; Presented by TA. Nada Kamel If no value in the square brackets, you have to initialize the array to have a size in the memory. // result = 16+2+77+40+70 = 205
  • 10. Problem 1 Write a C++ program that takes a positive integer value for n (not more than 55) and computes the sum of the following series: 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + ...... + n Presented by TA. Nada Kamel
  • 11. int n; cin >> n; if (n < 1 || n > 55) cout << “Please enter a positive number.n”; else { int values[55], sum = 0; for (int j = 0; j < n; j++) { if(j == 0 || j == 1) values[j] = 1; else values[j] = values[j-2] + values[j-1]; sum += values[j]; } cout << “Sum of value(s) is ” << sum << endl; }
  • 12. Problem 2 Trace the following code int j; int one[5], two[10]; for (j = 0; j < 5; j++) one[j] = 5 * j + 3; cout << “One contains: ”; for (j = 0; j < 5; j++) cout << setw(5) << one[j]; cout << endl; for (j = 0; j < 5; j++) { two[j] = 2*one[j] - 1; two[j + 5] = one[4 - j] – 1 + two [j]; } cout << “Two contains: ”; for (j = 0; j < 10; j++) cout << setw(5) << two[j]; cout << endl;
  • 13. Output One contains: 3 8 13 18 23 Two contains: 5 15 25 35 45 27 32 37 42 47 Presented by TA. Nada Kamel
  • 14. Any Questions? Presented by TA. Nada Kamel