Array 9/8/2018.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Programming and Data Structure
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Array.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Arrays- Part 2 Spring 2013Programming and Data Structure1.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Arrays in C.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT 10 Multidimensional Arrays.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wednesday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
1-d Arrays.
INC 161 , CPE 100 Computer Programming
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
Functions and Pointers
Lecture 7 Arrays 1. Concept of arrays Array and pointers
CHP-2 ARRAYS.
Hassan Khosravi / Geoffrey Tien
EKT120 : Computer Programming
MULTI-DIMENSIONAL ARRAY
Numeric Arrays Numeric Arrays Chapter 4.
Functions Dr. Sajib Datta
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
INC 161 , CPE 100 Computer Programming
Module 2 Arrays and strings – example programs.
Functions and Pointers
Pointers and Arrays Chapter 12
CS1100 Computational Engineering
Multidimensional Arrays
Programming and Data Structures
CNG 140 C Programming (Lecture set 8)
Lecture 10 Arrays.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
EKT150 : Computer Programming
Declaration, assignment & accessing
Review of Arrays and Pointers
EKT120: Computer Programming
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Outline Defining and using Pointers Operations on pointers
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
2-d Arrays.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Data Structures (CS212D) Week # 2: Arrays.
Initializing variables
Exercise Arrays.
DATA TYPES There are four basic data types associated with variables:
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

Array 9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

9/8/2018

Contd. For example, the array marks can be initialized while declaring using this statement.                                   int marks[5]={51,62,43,74,55}; The elements of the array marks can be referred to as marks [0], marks [1],marks [2], marks [3] and marks [4], respectively. The memory representation of the array marks is shown in Figure. As each element is of the type int (that is, 2 bytes long), the array marks occupies ten contiguous bytes in memory and these bytes are reserved in the memory at the compile-time. 9/8/2018

Problem double percentage[50]; If this array percentage is stored in memory starting from address 3000. then what will be the address of percentage[20] element. Ans: 3000+20*8= 3160 What will be size of array? Ans:400 9/8/2018

9/8/2018

Example 1: Find the minimum of a set of 10 numbers #include <stdio.h> main(){ int a[10], i, min; printf(“Enter 10 numbers:”); for (i=0; i<10; i++) scanf (“%d”, &a[i]); min = a[0]; for (i=1; i<10; i++){ if (a[i] < min) min = a[i]; } printf (“\n Minimum is %d”, min); 9/8/2018

Alternate Version1 for last problem #include <stdio.h> #define SIZE 10 main(){ int a[SIZE], i, min; printf(“Enter 10 numbers:”); for (i=0; i< SIZE; i++) scanf (“%d”, &a[i]); min = a[0]; for (i=1; i< SIZE; i++){ if (a[i] < min) min = a[i]; } printf (“\n Minimum is %d”, min); 9/8/2018

Alternate Version2 for last problem #include <stdio.h> main(){ int a[500], i, min; printf(“Enter number of elements:”); scanf(“%d”,&n); printf(“Enter n numbers:”); for (i=0; i< n; i++) scanf (“%d”, &a[i]); min = a[0]; for (i=1; i< n; i++){ if (a[i] < min) min = a[i]; } printf (“\n Minimum is %d”, min); 9/8/2018

Things you can not do with array You cannot use = to assign one array variable to another E.g. a = b; is illegal where a and b are arrays. You cannot use == to directly compare array variables e.g. if (a = = b) is illegal. You cannot use directly scanf or printf arrays printf (“…….”, a); is illegal. 9/8/2018

How to copy the elements of one array to another? By copying individual elements for (j=0; j<25; j++) a[j] = b[j]; 9/8/2018

How to read the elements of an array? By reading them one element at a time for (j=0; j<25; j++) scanf (“%f”, &a[j]); The ampersand (&) is necessary. 9/8/2018

9/8/2018

Problem- linear search in one-D array Write a program that asks the user to enter n integers of an array and an integer x. The program must search if n is in the array of n integers. Following is the sample output 9/8/2018

void main() { int array[100], search, c, n; printf("Enter the number of elements in array\n"); scanf("%d",&n);   printf("Enter %d integer(s)\n", n);   for (c = 0; c < n; c++) scanf("%d", &array[c]);   printf("Enter the number to search\n"); scanf("%d", &search);   for (c = 0; c < n; c++) { if (array[c] == search){ /* if required element found */ printf("%d is present at location %d.\n", search, c+1); break; } if (c == n) printf("%d is not present in array.\n", search);   9/8/2018

Sort one-D array and find its median void main(){ int i,j,n,temp,arr[100]; printf(“Enter n:”); for(i=0;i<n;i++) scanf(“%d”,&arr[i]); for(i=0; i<n-1; i++) { for(j=i+1; j<n; j++) { if(arr[i] > arr[j]) { // swap elements temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } if(n%2==0) // if there is an even number of elements, median will be mean of the two elements in the middle printf(“median is=%d”,(x[n/2] + x[n/2 - 1]) / 2.0); else //else return the element in the middle printf(“median is=%d”,x[n/2]);

9/8/2018

9/8/2018

9/8/2018

Initialization of Two-Dimensional Array Like a single-dimensional array, a two-dimensional array can also be declared and initialized at the same time. To understand the concept of two-dimensional array initialization, consider this statement.  int a[3] [2]={                      {101,51},          {l02,67},                      {l03,76} } 9/8/2018

Initialization of Two-Dimensional Array In last statement, an array of type int having three rows and two columns is declared and initialized. This type of initialization is generally used to increase the readability. However, the array a can also be initialized using this statement.   int a[] [2]={101,51,l02,67,l03,76}; In this statement, the first two elements are stored in the first row, next two in the second row and so on. Note that while initializing a two-dimensional array, mentioning the row size is optional, however, the column size must be specified. 9/8/2018

9/8/2018

How to read the elements of a 2-D array? • By reading them one element at a time for (i=0; i<nrow; i++) for (j=0; j<ncol; j++) scanf (“%f”, &a[i][j]); • The ampersand (&) is necessary. 9/8/2018

9/8/2018

9/8/2018

9/8/2018

int a[] [2]={101,51,l02,67,l03,76}; For example, the elements of array are referred to as a [0] [0], a [0] [1], a [1] [0], a [1] [1], a [2] [0] and a [2] [1] respectively. Generally, two-dimensional arrays are represented with the help of a matrix. However, in actual implementation, two-dimensional arrays are always allocated contiguous blocks of memory. Figure shows the matrix and memory representation of two-dimensional array a. 9/8/2018

Problem long double array[10][15]; If base address is 2000 then what will be the address of Array[5][6] element; Ans: 2000+(5*15+6)*10=2810 9/8/2018

C program to find average marks obtained by a class of 30 students in a test. 9/8/2018

9/8/2018

Example: Matrix Addition 9/8/2018

Example: Matrix Addition 9/8/2018

Indenting C Programs  Why Indentation is Important: Good style is about making your program Clear Understandable and Easily modifiable.  Use proper comment in program for better readability. There are so many Indenting Styles like Kernel style, K&R style and ANSI Style. We will follow ANSI Style(also known as Allman style). 9/8/2018

Rules of ANSI Style  This style puts the brace associated with a control statement(if,while,for, etc.) on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level(indented by one tab). Following is the code segment without proper indentation while (x == y) { printf(“x and y are same”); printf(“Next What to Do!”); } Same code segment in ANSI style would be while (x == y) { 9/8/2018

Some Examples of Bad vs Good coding style Code without proper indentation: void main(){ int a=40000,b=20000; if(a<b) printf(“Wow!”); else printf(“Really!”); } Same code segment in ANSI style would be void main() { if(a<b) // comparison of a and b printf(“Wow!”); else printf(“Really!”); 9/8/2018

Some Examples of Bad vs Good coding style Code without proper indentation: #include<stdio.h> void main(){ int row,col; for (row=1; row<=5; row++) { for (col=1; col<=5; col++) { if(row==1 || row==5 || col==1 || col==5) printf("*"); else printf(" "); } printf("\n"); 9/8/2018

Some Examples of Bad vs Good coding style Same code segment in ANSI style would be: void main() { int row,col; for (row=1; row<=5; row++) //outer Loop for (col=1; col<=5; col++) //Inner Loop if(row==1 || row==5 || col==1 || col==5) printf("*"); else printf(" "); } printf("\n"); 9/8/2018

O/P? main() { int arr[]=(12,13,14,15,16}; printf(“\n%d , %d”,sizeof(arr), sizeof(arr[0])); } O/p 10,2 9/8/2018

O/P? main() { float a[]= { 12.4, 2.3, 4.5, 6.7}; printf(“\n %d”, sizeof(a)/sizeof(a[0])); } O/p 16/4=4 9/8/2018

O/p? main( ) { int three[3][ ] = { 2, 4, 3, 6, 8, 2, 2, 3 ,1 } ; printf ( "\n%d", three[1][1] ) ; } Syntax error 9/8/2018

Homework Multiply two matrices of orders m*n and n*p respectively. 9/8/2018