ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS

Slides:



Advertisements
Similar presentations
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Computer Science 210 Computer Organization Pointers.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lesson #7 Arrays‏.
Lecture 5 Pointers 1. Variable, memory location, address, value
Stack and Heap Memory Stack resident variables include:
(Numerical Arrays of Multiple Dimensions)
(Numerical Arrays of Multiple Dimensions)
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Numeric Arrays Numeric Arrays Chapter 4.
Functions Dr. Sajib Datta
Array 9/8/2018.
Pointers.
INC 161 , CPE 100 Computer Programming
Module 2 Arrays and strings – example programs.
Arrays in C.
Pointers.
Computer Science 210 Computer Organization
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Arrays And Functions.
ICS103 Programming in C Lecture 13: Arrays II
(Numerical Arrays of Multiple Dimensions)
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
A function with one argument
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions with arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pointers.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Intro to Arrays Storing List of Data.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays and Pointers CSE 2031 Fall July 2019.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Introduction to Pointers
Presentation transcript:

ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS

Memory Map – What happens after an array is declared & initialized to zero Address Memory Array element 2400 List[0] 2402 List[1] 2404 List[2] 2406 List[3] 2408 List[4]

Review #include <stdio.h> #define MAX_SIZE 10 int main(void) { int i, sum, x[MAX_SIZE]; sum = 0; printf("Enter %d integers=> ", MAX_SIZE); for (i = 0; i < MAX_SIZE; ++i) { scanf("%d", &x[i]); sum += x[i]; } printf(" n %% of total\n"); for (i = 0; i < MAX_SIZE; ++i) printf("%2d%10.2f\n", x[i], (double)x[i] / sum * 100); return (0);

Arrays and pointers For arrays, only the address of the first cell is important as all cells are stored together in the machine. x[1] is stored next-door to x[0]. The array name without any subscript is the address of the first cell (but is not actually a pointer as you cannot modify it). If I have an array declared int temp[4];, temp is the same thing as &temp[0]. temp, as we know, is the address of temp [0], but temp+1 is the address of temp [1], temp+2 the address of temp [2],and so on...

Arrays and cells temp being an array, a statement like temp =70;would be illegal as temp would be an address, not a value. With a value, you must always mention which cell to fill, so temp[3]=70; would be correct. The sizeof operator will return the size (in bytes) of the array. printf ("%d", sizeof (temp)); would display 16 on a 32-bit system (4 cells of integers at 32 bits or 4 bytes each).

How to Initialize Arrayes? /* PROGRAM #70 */ /* INITIALIZE TO NON-ZERO */ /* INITIALIZE AT THE TIME OF DECLARATION*/ #include <stdio.h> #define ListSize 5 main( ){ int i; /* Declare and initialize array */ int List[ListSize] = {2, 4, 6, 8, 10}; /*Print content of elements of array */ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %3d.\n", i, List[i]); }

Memory Map – What happens after an array is declared & initialized to non-zero values Address Memory Array element 2400 2 List[0] 2402 4 List[1] 2404 6 List[2] 2406 8 List[3] 2408 10 List[4]

INITIALIZE TO NON-ZERO /* PROGRAM # 71 */ /* ASK FOR USER’S INPUT */ #include <stdio.h> #define ListSize 5 main( ){ int i; int List[ListSize]; /* Input data from user */ for(i=0; i<ListSize; i++){ printf(“Pls enter List[%d]= ”, i); scanf(“%d”, &List[i]); } /* Print array contents */ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %d.\n", i, List[i]);

/* GOOD USAGE OF INDEX */ /* PROGRAM # 72 */ #include <stdio.h> #define asize 10 main( ){ int i, vector[asize]; /* Input array contents from user */ for(i=0; i<asize; i++){ printf("Pls enter vector[%d]: \n",i); scanf("%d",&vector[i]); } /* Print array contents */ for(i= 0; i < asize; i++) printf("vector[%d] = %d\n" , i ,vector[i]);

Q? Write a program which fills an array from a file.

/* Problem: This program fills an array from a file –ar#3*/ #include <stdio.h> int main (void) { int numbers[10], i; FILE *input; input = fopen("numbers.txt", "r"); /* reading file - filling array */ for (i=0; i<10; ++i) fscanf(input, "%d", &numbers[i]); /* printing the content of array */ printf("The numbers read are: "); printf("%4d", numbers[i]); printf ("\n"); fclose (input); return (0); }

Q? /* Input data from user */ /* Print array contents using pointer*/

Arrays and Pointers /* PROGRAM # 73 */ /* POINTERS & ARRAYS */ Pointers can be used to access the contents of elements of an array. /* PROGRAM # 73 */ /* POINTERS & ARRAYS */ #include <stdio.h> #define ListSize 5 main( ){ int i; int List[ListSize]; /* Input data from user */ for(i=0; i<ListSize; i++){ printf(“Pls enter List[%d]= ”, i); scanf(“%d”, &List[i]); } /* Print array contents using pointer*/ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %d.\n", i, *(List+i));

Working with an array using pointers Address Pointers memory Array element 2400 *(List+0) 2 List[0] 2402 *(List+1) 4 List[1] 2404 *(List+2) 6 List[2] 2406 *(List+3) 8 List[3] 2408 *(List+4) 10 List[4]

NOTE!!! Name of the array is a pointer that’s holding the address of the top of the array, i.e., the address of the first element of the array. Knowing the name of the array and the size of the array, we can access all elements of the array. Pointer is defined as type integer. Incrementing the pointer by one each time, will actually increment it by two since integers take 2 bytes in memory(8bit).

Sending arrays to functions Sending an array to a function is like sending multiple values all at once. The best way is to send the address (the array name alone) to the function so that the function can work with the original array stored in the calling function (main). Let's have a function that finds the largest value in an array of integers and returns the cell number where it was found. Note that we must always send the size of the array as an argument because the function will have no way of knowing it otherwise.

Passing Numeric Arrays between calling function & the function /* PASSING ARRAYS TO FUNCTIONS *//* PROGRAM # 74 */ #include <stdio.h> #define asize 5 void Get_List(int list[ ]); void Print_List(int list[ ]); main( ){ int vector[asize]; /* Call functions */ Get_List(vector); Print_List(vector); } /* To get the elements of the array from the user */ void Get_List(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To printout the elements of the array */ void Print_List(int list[ ]){ for(i=0; i<asize; i++) printf("list[%d] is %d\n", i, list[i]);

NOTE!!! When using an array as the argument of a function, we need to use [ ] to indicate that the argument is an array. In this case, we don’t need to mention the size of the array. If we want, we can mention the size of the array. Name of the array is a pointer that points to the 1st element of the array.

Q? /* PRINTING AN ARRAY IN REVERSE ORDER */

Manipulation of Array Index /* PRINTING AN ARRAY IN REVERSE ORDER *//* PROGRAM # 75 */ #include <stdio.h> #define asize 10 void Get_List(int list[ ]); void Print_Backward(int list[ ]); main( ){ /* Declare array */ int vector[asize]; /* Call to the functions */ Get_List(vector); Print_Backward(vector); } /* To get the array from the user */ void Get_List(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To print the elements of the array in reverse order */ void Print_Backward(int list[ ]){ printf("\n\nthe entered numbers displayed \n"); printf("in the reverse order of entry:\n"); for(i = asize - 1; i >= 0; i--) printf("List[%d] = %d\n", i, list[i]);

NOTE!!! In this program, we DON’T make any changes to the array and its placement in the memory; we simply PRINT the array in reverse order.

REVIEW

/* GOOD USAGE OF INDEX */ /* PROGRAM # 72 */ #include <stdio.h> #define asize 10 main( ){ int i, vector[asize]; /* Input array contents from user */ for(i=0; i<asize; i++){ printf("Pls enter vector[%d]: \n",i); scanf("%d",&vector[i]); } /* Print array contents */ for(i= 0; i < asize; i++) printf("vector[%d] = %d\n" , i ,vector[i]);

Q? Write a program which fills an array from a file.

/* Problem: This program fills an array from a file –ar#3*/ #include <stdio.h> int main (void) { int numbers[10], i; FILE *input; input = fopen("numbers.txt", "r"); /* reading file - filling array */ for (i=0; i<10; ++i) fscanf(input, "%d", &numbers[i]); /* printing the content of array */ printf("The numbers read are: "); printf("%4d", numbers[i]); printf ("\n"); fclose (input); return (0); }

Q? /* Input data from user */ /* Print array contents using pointer*/

Arrays and Pointers /* PROGRAM # 73 */ /* POINTERS & ARRAYS */ Pointers can be used to access the contents of elements of an array. /* PROGRAM # 73 */ /* POINTERS & ARRAYS */ #include <stdio.h> #define ListSize 5 main( ){ int i; int List[ListSize]; /* Input data from user */ for(i=0; i<ListSize; i++){ printf(“Pls enter List[%d]= ”, i); scanf(“%d”, &List[i]); } /* Print array contents using pointer*/ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %d.\n", i, *(List+i));

Working with an array using pointers Address Pointers memory Array element 2400 *(List+0) 2 List[0] 2402 *(List+1) 4 List[1] 2404 *(List+2) 6 List[2] 2406 *(List+3) 8 List[3] 2408 *(List+4) 10 List[4]

NOTE!!! Name of the array is a pointer that’s holding the address of the top of the array, i.e., the address of the first element of the array. Knowing the name of the array and the size of the array, we can access all elements of the array. Pointer is defined as type integer. Incrementing the pointer by one each time, will actually increment it by two since integers take 2 bytes in memory(8bit).

Sending arrays to functions Sending an array to a function is like sending multiple values all at once. The best way is to send the address (the array name alone) to the function so that the function can work with the original array stored in the calling function (main). Let's have a function that finds the largest value in an array of integers and returns the cell number where it was found. Note that we must always send the size of the array as an argument because the function will have no way of knowing it otherwise.

NOTE!!! When using an array as the argument of a function, we need to use [ ] to indicate that the argument is an array. In this case, we don’t need to mention the size of the array. If we want, we can mention the size of the array. Name of the array is a pointer that points to the 1st element of the array.

Q? /* PRINTING AN ARRAY IN REVERSE ORDER */

Manipulation of Array Index /* PRINTING AN ARRAY IN REVERSE ORDER *//* PROGRAM # 75 */ #include <stdio.h> #define asize 10 void Get_List(int list[ ]); void Print_Backward(int list[ ]); main( ){ /* Declare array */ int vector[asize]; /* Call to the functions */ Get_List(vector); Print_Backward(vector); } /* To get the array from the user */ void Get_List(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To print the elements of the array in reverse order */ void Print_Backward(int list[ ]){ printf("\n\nthe entered numbers displayed \n"); printf("in the reverse order of entry:\n"); for(i = asize - 1; i >= 0; i--) printf("List[%d] = %d\n", i, list[i]);

NOTE!!! In this program, we DON’T make any changes to the array and its placement in the memory; we simply PRINT the array in reverse order.

Q? /* FIND MINIMUM IN AN ARRAY */

Finding MIN Value /* FIND MINIMUM IN AN ARRAY *//* PROGRAM # 76 */ #include <stdio.h> #define asize 8 void GetList(int list[ ]); /* Declare functions */ int MinimumValue(int list[ ]); main( ){ int vector[asize]; int index; GetList(vector); printf("The smallest number in the list is %d\n", MinimumValue(vector) ); } /* To get the array from the user */ void GetList(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To find the minimum */ int MinimumValue(int list[ ]){ int i, minimum; minimum = list[0]; for(i = 1; i < asize; i++) if(list[i] < minimum) minimum = list[i]; return(minimum);