SlideShare a Scribd company logo
1.1 Nested Loops – Lab3C.cpp
Loops often have loops inside them. These are called “nested”
loops. In this exercise you will finish a nested loop that reads
lines of numbers from a file and computes and prints out the
sum of each line.
//C++ Lab3C.cpp
// <Your Name>
// <Your Section>
// <Your student id>
//1. [include required header files]//
using namespace std;
int main()
{
[2. Declare required variables]
try
{
//3> put your file name & Open file as an input mode
ifstream Openfile(" “);
//4> If file doesn't exist then throw error number
if(Openfile.good())
{
while( getline(Openfile, sLine)) //Outer While Loop
Condition
{
cout << "The Contents of line sLine" << sLine <<
"n";
stringstream Str(sLine);
while (Str >> temp ) //Inner While Loop
Condition
{
cout << "String ~to double" << temp;
}// Inner while Loop
} //Outer While Loop
Openfile.close();
//3> Catch the error number and display message
}
else {
throw 10;
}
} //if
catch(int e)
{
cout << "File Not found " << e << endl;
}
//system("pause"); //Pause
return 0;
}//main
1.2 Once you have completed the program, run it on the input
file below:
Lab4C.in
10 20 30 40 50
60 70 80 90.0
11 13.0
20 40 70 19.0
Lab4C.out or Lab4C.doc with screen shot
Read Line 0 10.0 Sum 10.0
Read Line 0 20.0 Sum 30.0
Read Line 0 30.0 Sum 60.0
Read Line 0 40.0 Sum 100.0
Read Line 0 50.0 Sum 150.0
…..
Read Line 3 20.0 Sum 20.0
Read Line 3 40.0 Sum 60.0
Read Line 3 70.0 Sum 130.0
Read Line 3 19.0 Sum 149.0
1.3 Upload your four files (Lab3A.cpp, Lab3B.cpp, Lab3C.cpp
and Lab3C.doc) as one single zip file named “Lab3ABC.zip”)
2. Lab3D – Two Dimensional Arrays
2.1 Declaring 2D arrays
In C++, to make a 2D array, we simply declare an array where
the type is an array. To see how this works, first look at how we
create an int array with 10 elements. One might type
int arr1D[ 10] ;
What the above code actually does is create a new “int Array”
that reserves enough space to store 10 integers. For creating a
2D arrays, we can make an array of them, by appending another
set of square brackets:
int arr2D[3][7];
This declares a two dimensional array of ints with 3 rows and 7
columns. By convention, we think of the array as being indexed
in “Row Major Order”, which means that the row (y value)
comes before the column (x value).
Thus, visually, the array looks like this:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
The top left element is at position [0][0], and the bottom right
element is at position [2][6]. The array elements are initialized
just as in the 1D case ( Numerics are set to 0, and object types
are set to null). To access one of the elements of a 2D array, we
use double bracket notation again, so we can write
arr2D[0][1] = 1;
arr2D[2][3] = 4;
This would give us the array:
0 1 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 4 0 0 0
If we want to visit or process every element of a 2D array, we
need a nested for loop to do it. Generally, when you iterate over
a 2D array, you want to do it in row major order, because this is
more memory efficient. The reason why is that adjacent
elements in a row are stored contiguously (one after another) in
memory. Another reason, for an English speaker, is that this is
the same order your read a book—top to bottom, left to right.
The following code iterates over arr2D, changing each value to
1:
const int ROW =3;
const int COL =7;
int arr2D[3][7];
for(int r=0; r < ROW ; r ++)
{
for(int c =0; COL; r++)
{
arr2D[r][c] = 1;
}
}
I'm using the variables "r" and "c" because they iterate of the
rows and columns of the array, respectively. The outer loop
iterates over all the rows, and the inner loop iterates over all the
columns in a single row. Notice how the row index (r) comes
before the column index (c).
Also, notice how the loop bounds are handled in the interior
loop. While the outer loop goes from 0 to ROW, the inner loop
goes from 0 to COL.
Type in, and finish the following program, which computes and
prints an addition table using nested loops and a 2D array:
//C++ Lab3B.cpp
// <Your Name>
// <Your Section>
// <Your student id>
#include <iostream>
using namespace std;
typedef int* IntArrayPtr;
int main( )
{
//Propt user for input
cout << "Please Enter your Size of An Array n";
//Read an integer from the keyboard & save it to the int
variable.
//[1. Add Code Here ]
//[2. Create one-dimensional dynamic array]
//[3. Create two-dimensional dynamic array]
//Fill out the table using a netsted loop so that
//Table[r][c] = r + c;
//[4. Add Code Here]
//Itrate over the table, printing each value
//so that columns align(hint: use a nested loop )
//[5. Add Code Here]
//[6. Delete dynamic array]
system("pause");
return 0;
}//End of main
When finished, with an input value of 4, the program should
print something like:
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6

More Related Content

Similar to 1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx

C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
 
2ds
2ds2ds
Arrays
ArraysArrays
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
Mahmoud Samir Fayed
 
UNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt pptUNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt ppt
SuganthiDPSGRKCW
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Kashif Nawab
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
Mohamed Abdallah
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
AnkurRajSingh2
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptx
DilanAlmsa
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
Team-VLSI-ITMU
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 

Similar to 1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx (20)

C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
2ds
2ds2ds
2ds
 
Arrays
ArraysArrays
Arrays
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
 
UNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt pptUNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt ppt
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptx
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Array assignment
Array assignmentArray assignment
Array assignment
 

More from christiandean12115

100 Original WorkZero PlagiarismGraduate Level Writing Required.docx
100 Original WorkZero PlagiarismGraduate Level Writing Required.docx100 Original WorkZero PlagiarismGraduate Level Writing Required.docx
100 Original WorkZero PlagiarismGraduate Level Writing Required.docx
christiandean12115
 
10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx
10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx
10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx
christiandean12115
 
10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx
10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx
10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx
christiandean12115
 
10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx
10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx
10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx
christiandean12115
 
10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx
10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx
10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx
christiandean12115
 
10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST M.docx
10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST  M.docx10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST  M.docx
10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST M.docx
christiandean12115
 
10.1 What are three broad mechanisms that malware can use to propa.docx
10.1 What are three broad mechanisms that malware can use to propa.docx10.1 What are three broad mechanisms that malware can use to propa.docx
10.1 What are three broad mechanisms that malware can use to propa.docx
christiandean12115
 
10.0 ptsPresentation of information was exceptional and included.docx
10.0 ptsPresentation of information was exceptional and included.docx10.0 ptsPresentation of information was exceptional and included.docx
10.0 ptsPresentation of information was exceptional and included.docx
christiandean12115
 
10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx
10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx
10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx
christiandean12115
 
10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx
10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx
10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx
christiandean12115
 
10 What does a golfer, tennis player or cricketer (or any othe.docx
10 What does a golfer, tennis player or cricketer (or any othe.docx10 What does a golfer, tennis player or cricketer (or any othe.docx
10 What does a golfer, tennis player or cricketer (or any othe.docx
christiandean12115
 
10 September 2018· Watch video· Take notes withfor students.docx
10 September 2018· Watch video· Take notes withfor students.docx10 September 2018· Watch video· Take notes withfor students.docx
10 September 2018· Watch video· Take notes withfor students.docx
christiandean12115
 
10 Research-Based Tips for Enhancing Literacy Instruct.docx
10 Research-Based Tips for Enhancing Literacy Instruct.docx10 Research-Based Tips for Enhancing Literacy Instruct.docx
10 Research-Based Tips for Enhancing Literacy Instruct.docx
christiandean12115
 
10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx
10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx
10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx
christiandean12115
 
10 Most Common Err.docx
10 Most Common Err.docx10 Most Common Err.docx
10 Most Common Err.docx
christiandean12115
 
10 Introduction Ask any IT manager about the chall.docx
10 Introduction Ask any IT manager about the chall.docx10 Introduction Ask any IT manager about the chall.docx
10 Introduction Ask any IT manager about the chall.docx
christiandean12115
 
10 Customer Acquisition and Relationship ManagementDmitry .docx
10 Customer Acquisition and Relationship ManagementDmitry .docx10 Customer Acquisition and Relationship ManagementDmitry .docx
10 Customer Acquisition and Relationship ManagementDmitry .docx
christiandean12115
 
10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​ (seri.docx
10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​  (seri.docx10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​  (seri.docx
10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​ (seri.docx
christiandean12115
 
10 ers. Although one can learn definitions favor- able to .docx
10 ers. Although one can learn definitions favor- able to .docx10 ers. Although one can learn definitions favor- able to .docx
10 ers. Although one can learn definitions favor- able to .docx
christiandean12115
 
10 academic sources about the topic (Why is America so violent).docx
10 academic sources about the topic (Why is America so violent).docx10 academic sources about the topic (Why is America so violent).docx
10 academic sources about the topic (Why is America so violent).docx
christiandean12115
 

More from christiandean12115 (20)

100 Original WorkZero PlagiarismGraduate Level Writing Required.docx
100 Original WorkZero PlagiarismGraduate Level Writing Required.docx100 Original WorkZero PlagiarismGraduate Level Writing Required.docx
100 Original WorkZero PlagiarismGraduate Level Writing Required.docx
 
10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx
10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx
10.11771066480704270150THE FAMILY JOURNAL COUNSELING AND THE.docx
 
10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx
10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx
10.11771066480703252339 ARTICLETHE FAMILY JOURNAL COUNSELING.docx
 
10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx
10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx
10.11770022427803260263ARTICLEJOURNAL OF RESEARCH IN CRIME AN.docx
 
10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx
10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx
10.11770022487105285962Journal of Teacher Education, Vol. 57,.docx
 
10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST M.docx
10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST  M.docx10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST  M.docx
10.11770011000002250638ARTICLETHE COUNSELING PSYCHOLOGIST M.docx
 
10.1 What are three broad mechanisms that malware can use to propa.docx
10.1 What are three broad mechanisms that malware can use to propa.docx10.1 What are three broad mechanisms that malware can use to propa.docx
10.1 What are three broad mechanisms that malware can use to propa.docx
 
10.0 ptsPresentation of information was exceptional and included.docx
10.0 ptsPresentation of information was exceptional and included.docx10.0 ptsPresentation of information was exceptional and included.docx
10.0 ptsPresentation of information was exceptional and included.docx
 
10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx
10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx
10-K1f12312012-10k.htm10-KUNITED STATESSECURIT.docx
 
10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx
10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx
10-K 1 f12312012-10k.htm 10-K UNITED STATESSECURITIES AN.docx
 
10 What does a golfer, tennis player or cricketer (or any othe.docx
10 What does a golfer, tennis player or cricketer (or any othe.docx10 What does a golfer, tennis player or cricketer (or any othe.docx
10 What does a golfer, tennis player or cricketer (or any othe.docx
 
10 September 2018· Watch video· Take notes withfor students.docx
10 September 2018· Watch video· Take notes withfor students.docx10 September 2018· Watch video· Take notes withfor students.docx
10 September 2018· Watch video· Take notes withfor students.docx
 
10 Research-Based Tips for Enhancing Literacy Instruct.docx
10 Research-Based Tips for Enhancing Literacy Instruct.docx10 Research-Based Tips for Enhancing Literacy Instruct.docx
10 Research-Based Tips for Enhancing Literacy Instruct.docx
 
10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx
10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx
10 Strategic Points for the Prospectus, Proposal, and Direct Pract.docx
 
10 Most Common Err.docx
10 Most Common Err.docx10 Most Common Err.docx
10 Most Common Err.docx
 
10 Introduction Ask any IT manager about the chall.docx
10 Introduction Ask any IT manager about the chall.docx10 Introduction Ask any IT manager about the chall.docx
10 Introduction Ask any IT manager about the chall.docx
 
10 Customer Acquisition and Relationship ManagementDmitry .docx
10 Customer Acquisition and Relationship ManagementDmitry .docx10 Customer Acquisition and Relationship ManagementDmitry .docx
10 Customer Acquisition and Relationship ManagementDmitry .docx
 
10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​ (seri.docx
10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​  (seri.docx10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​  (seri.docx
10 ELEMENTS OF LITERATURE (FROM A TO Z)   1  ​PLOT​ (seri.docx
 
10 ers. Although one can learn definitions favor- able to .docx
10 ers. Although one can learn definitions favor- able to .docx10 ers. Although one can learn definitions favor- able to .docx
10 ers. Although one can learn definitions favor- able to .docx
 
10 academic sources about the topic (Why is America so violent).docx
10 academic sources about the topic (Why is America so violent).docx10 academic sources about the topic (Why is America so violent).docx
10 academic sources about the topic (Why is America so violent).docx
 

Recently uploaded

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 

Recently uploaded (20)

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 

1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx

  • 1. 1.1 Nested Loops – Lab3C.cpp Loops often have loops inside them. These are called “nested” loops. In this exercise you will finish a nested loop that reads lines of numbers from a file and computes and prints out the sum of each line. //C++ Lab3C.cpp // <Your Name> // <Your Section> // <Your student id> //1. [include required header files]// using namespace std; int main() { [2. Declare required variables] try { //3> put your file name & Open file as an input mode ifstream Openfile(" “); //4> If file doesn't exist then throw error number if(Openfile.good()) { while( getline(Openfile, sLine)) //Outer While Loop Condition { cout << "The Contents of line sLine" << sLine <<
  • 2. "n"; stringstream Str(sLine); while (Str >> temp ) //Inner While Loop Condition { cout << "String ~to double" << temp; }// Inner while Loop } //Outer While Loop Openfile.close(); //3> Catch the error number and display message } else { throw 10; } } //if catch(int e) { cout << "File Not found " << e << endl; } //system("pause"); //Pause return 0; }//main 1.2 Once you have completed the program, run it on the input file below: Lab4C.in 10 20 30 40 50
  • 3. 60 70 80 90.0 11 13.0 20 40 70 19.0 Lab4C.out or Lab4C.doc with screen shot Read Line 0 10.0 Sum 10.0 Read Line 0 20.0 Sum 30.0 Read Line 0 30.0 Sum 60.0 Read Line 0 40.0 Sum 100.0 Read Line 0 50.0 Sum 150.0 ….. Read Line 3 20.0 Sum 20.0 Read Line 3 40.0 Sum 60.0 Read Line 3 70.0 Sum 130.0 Read Line 3 19.0 Sum 149.0 1.3 Upload your four files (Lab3A.cpp, Lab3B.cpp, Lab3C.cpp and Lab3C.doc) as one single zip file named “Lab3ABC.zip”) 2. Lab3D – Two Dimensional Arrays 2.1 Declaring 2D arrays In C++, to make a 2D array, we simply declare an array where the type is an array. To see how this works, first look at how we create an int array with 10 elements. One might type int arr1D[ 10] ; What the above code actually does is create a new “int Array” that reserves enough space to store 10 integers. For creating a 2D arrays, we can make an array of them, by appending another set of square brackets: int arr2D[3][7]; This declares a two dimensional array of ints with 3 rows and 7 columns. By convention, we think of the array as being indexed in “Row Major Order”, which means that the row (y value)
  • 4. comes before the column (x value). Thus, visually, the array looks like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The top left element is at position [0][0], and the bottom right element is at position [2][6]. The array elements are initialized just as in the 1D case ( Numerics are set to 0, and object types are set to null). To access one of the elements of a 2D array, we use double bracket notation again, so we can write arr2D[0][1] = 1; arr2D[2][3] = 4; This would give us the array: 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 If we want to visit or process every element of a 2D array, we need a nested for loop to do it. Generally, when you iterate over a 2D array, you want to do it in row major order, because this is more memory efficient. The reason why is that adjacent elements in a row are stored contiguously (one after another) in memory. Another reason, for an English speaker, is that this is the same order your read a book—top to bottom, left to right. The following code iterates over arr2D, changing each value to 1: const int ROW =3; const int COL =7; int arr2D[3][7]; for(int r=0; r < ROW ; r ++) { for(int c =0; COL; r++) { arr2D[r][c] = 1;
  • 5. } } I'm using the variables "r" and "c" because they iterate of the rows and columns of the array, respectively. The outer loop iterates over all the rows, and the inner loop iterates over all the columns in a single row. Notice how the row index (r) comes before the column index (c). Also, notice how the loop bounds are handled in the interior loop. While the outer loop goes from 0 to ROW, the inner loop goes from 0 to COL. Type in, and finish the following program, which computes and prints an addition table using nested loops and a 2D array: //C++ Lab3B.cpp // <Your Name> // <Your Section> // <Your student id> #include <iostream> using namespace std; typedef int* IntArrayPtr; int main( ) { //Propt user for input cout << "Please Enter your Size of An Array n"; //Read an integer from the keyboard & save it to the int variable. //[1. Add Code Here ] //[2. Create one-dimensional dynamic array] //[3. Create two-dimensional dynamic array]
  • 6. //Fill out the table using a netsted loop so that //Table[r][c] = r + c; //[4. Add Code Here] //Itrate over the table, printing each value //so that columns align(hint: use a nested loop ) //[5. Add Code Here] //[6. Delete dynamic array] system("pause"); return 0; }//End of main When finished, with an input value of 4, the program should print something like: 0 1 2 3 1 2 3 4 2 3 4 5 3 4 5 6