CL103_Computer Programming_Lab # 03 [Arrays]

Publish in

Documents

139 views

Please download to get full document.

View again

of 20
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Share
Description
array
Tags
Transcript
  CL103 - Computer Programming   Lab # 03: Arrays in C/C++   Objectives:    Arrays’ Overview      Declaring Arrays      Initializing arrays      Accessing the values of an array      Default Initialization of Arrays      Sizeof operator for arrays      Operations of Array      Array & Functions      Parallel Arrays      Two Dimensional Arrays      Declaration of Two-Dimensional Array      Examples of Two Dimensional Arrays:      Default Initialization of 2-Dimensional Arrays      Two Dimensional Array in Functions      Multidimensional Arrays      Live Demonstration in Lab   Instructions: 1.   You are allowed to use your own laptops. 2.   You can consult the books, manuals and class lectures. 3.   You should have stationary like register and ballpoint to analyze the tasks first. 4.   Ensure that your environment is working properly. 5.   In practice session, consultation is allowed. 6.   No discussion is allowed during tasks solution. 7.   Attempt all questions yourself. 8.   Submission of all tasks’  solutions is necessary in any way. 9.   Tasks completed in specified time will be graded.  Page 2  of 20   Arrays’ Overview  CL103-Computer Programming By: Mr. Najeeb -Ur-Rehman   Lab # 03: Arrays in C/C++ Getting Started  Arrays ’ Overview   An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. An array is a group of items that can be identified as similar because they are of the same nature. Arrays come in two flavors: one dimensional and multi-dimensional array. Every one of the pictures above represents a single dimensional array. Declaring Arrays Figure 2: Array Layout   Figure 1: One-Dimensional Array   An array of airplanes An array of bugs An array of cards An array of characters  Page 3  of 20   Arrays’ Overview  CL103-Computer Programming By: Mr. Najeeb -Ur-Rehman   Lab # 03: Arrays in C/C++ int billy[5]; Initializing arrays billy[0]=16; billy[1]=2; billy[2]=77; billy[3]=40; billy[4]=12071;  Accessing the values of an array Default Initialization of Arrays You can give values to each array element when the array is first defined. // arrays example #include <iostream>  using namespace std; int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0; } /*Output: 12206 */ Here’s an example, DAYS, that sets 12 array elements in the array days_per_month to the number of days in each month. #include <iostream> #include <conio.h> using namespace std; void main() { int month, day, total_days;  Page 4  of 20   Arrays’ Overview  CL103-Computer Programming By: Mr. Najeeb -Ur-Rehman   Lab # 03: Arrays in C/C++ int days_per_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; cout << \nEnter month (1 to 12): ; cin >> month; cout << Enter day (1 to 31): ; cin >> day; total_days = day; //separate days for(int j=0; j<month-1; j++) //add days each month total_days += days_per_month[j]; cout << Total days from start of year is:  << total_days << endl; _getch(); } Enter month (1 to 12): 3 Enter day (1 to 31): 5 Total days from start of year is: 64 #include <iostream>  using namespace std; int main() { double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28}; cout << 2nd member =  << distance[1] << endl; cout << 5th member =  << distance[4] << endl; return 0; } /* 2nd member = 720.52 5th member = 6.28 */ Using this approach, each member of the array can have its value accessed. Here is an example: #include <iostream>  using namespace std; int main() { double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28}; cout << Distance 1:  << distance[0] << endl; cout << Distance 2:  << distance[1] << endl; cout << Distance 3:  << distance[2] << endl; cout << Distance 4:  << distance[3] << endl; cout << Distance 5:  << distance[4] << endl; return 0; }
Related Search

Previous Document

Cutting List.docx

Next Document

h0using

We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks