Two Dimensional Array - An array with a multiple indexs.

Similar documents
Two Dimensional Array - An array with a multiple indexs.

C: How to Program. Week /Apr/23

C++ PROGRAMMING SKILLS Part 4: Arrays

Arrays and Applications

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

C Arrays Pearson Education, Inc. All rights reserved.

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Computer Programming Lecture 14 Arrays (Part 2)

Computer Science & Engineering 150A Problem Solving Using Computers

Arrays. Systems Programming Concepts

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

Content. In this chapter, you will learn:

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Procedural Programming

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Principles of Programming. Chapter 6: Arrays

Solutions to Chapter 8

Fundamentals of Programming Session 14

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

C: How to Program. Week /Apr/16

Programming Assignment #4 Arrays and Pointers

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Chapter 9 Introduction to Arrays. Fundamentals of Java

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

'C' Programming Language

Introduction to C Final Review Chapters 1-6 & 13

Using pointers with functions

1. The keyword main in C language is used for

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

Module 6: Array in C

Array. Arrays. Declaring Arrays. Using Arrays

ECE264 Fall 2013 Exam 1, September 24, 2013

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Chapter 7 Array. Array. C++, How to Program

Programming for Engineers Arrays

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

C Functions. 5.2 Program Modules in C

Procedural programming with C

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Chapter 6 - Pointers

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

ECET 264 C Programming Language with Applications. C Program Control

APSC 160 Review Part 2

Function Call Stack and Activation Records

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Arrays. Eng. Mohammed Abdualal


Functions. Arash Rafiey. September 26, 2017

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Introduction to Computer Science Midterm 3 Fall, Points

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Arrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CS 108 Computing Fundamentals. October/November Array Bootcamp

Government Polytechnic Muzaffarpur.

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Programming for Engineers Functions

Write a C program using arrays and structure

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Object Oriented Programming. Java-Lecture 6 - Arrays

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

C Exercises. Ben Vandiver February 28, 2003

Fundamental of Programming (C)

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Arrays. Week 4. Assylbek Jumagaliyev

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Arrays in C. By Mrs. Manisha Kuveskar.

ECE264 Spring 2014 Exam 2, March 11, 2014

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Arrays and Strings. Arash Rafiey. September 12, 2017

Arrays and Pointers. CSE 2031 Fall November 11, 2013

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Abstract Data Type: Stack

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

by Pearson Education, Inc. All Rights Reserved.

CS 101, Spring 2016 March 22nd Exam 2

Pointers. 10/5/07 Pointers 1

Transcription:

LAB5 : Arrays Objectives: 1. To learn how to use C array as a counter. 2. To learn how to add an element to the array. 3. To learn how to delete an element from the array. 4. To learn how to declare two dimensional array. 5. To learn passing arrays as function parameters Definitions: Theory: Two Dimensional Array - An array with a multiple indexs. Multi-dimensional Arrays An array is a collection of individual values, all of the same data type, stored in adjacent memory locations. Using the array name together with an integral valued index in square brackets refers to the individual values. Multi-dimensional arrays have additional indices for each additional dimension. The index indicates the position of the individual component value within the collection of values. The index is also called the subscript. In C, the first array element always has subscript 0. The second array element has subscript 1, etc. The base address of an array is its beginning address in memory. Declaring a Multi-dimensional Array: In order to declare an m-dimensional array, a C programmer will use the following syntax below. DataType ArrayName[#of rows][#of columns]; The example below shows the declaration of a 2-dimensional integer array of size 3 x 3 with elements [0-2] [0-2]. #define MAXSIZE 3... int array[maxsize][maxsize]; A multi-dimensional array can be initialized during declaration by equating the array to a listing of the array's members in brackets. For example: (Note: Each dimension has its own brackets.) 1

int array[maxsize][maxsize] = 2, 4, 6, 8, 10, 12, 14, 16, 18; Passing Arrays as Function Parameters In C, arrays are always passed by reference by default. Whenever an array is passed as a parameter, its base address is sent to the called function. Generally, functions that work with arrays require 2 items of information as actual parameters: the beginning address of the array (in memory), and the number of elements to process in the array. For example (Function PrintArray): void PrintArray(int Array[], int ArraySize) for (int i = 0; i <ArraySize; i++) printf("array[%d] = %d\n",i,array[i]); Use of Const Since arrays are passed to functions by reference, the values contained in the array will be changed permanently. In order to prevent an array that is used as an actual parameter from being unintentionally changed by a function, you can place const in the function heading and prototype. NOTE: If the values of the array need to be changed in the function do not use const. 2

Assignment: 1) Complete the following program below by filling in the blanks. #include "stdlib.h" #include "time.h" #include "conio.h" #define SIZE 7 /* function main begins program execution */ void main() int face; /* random die value 1-6 */ int roll; /* roll counter */ int frequency[ SIZE ] = ; /* clear counts */ srand( time( NULL ) ); /* seed random-number generator */ //roll a die 10 times for ( roll = 1; roll <= ; roll++ ) face = ; //get a random value between 1 and 6 ; // increases the appropriate frequency counter depending on the value of face printf( "%s%17s\n", "Face", "Frequency" ); /* output frequency elements 1-6 in tabular format */ for ( face = 1; face < ; face++ ) printf( "%4d%17d\n", face, ); 3

2) Using array to summarize survey results 40 Students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means bad and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the survey. Summarize the number of responses of each type (1 through 10) The array responses is a 40-element array of students responses. The array frequency is a 11-element array to count the number of occurrences of each response. responses array elements are = 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 Hint: initialize frequency counter to zero. Initialize responses array elements 4

3) Complete and run the following program. This program creates and prints the multiplication table for 1 to 9 #define MAX 10 void main() int table[ ][MAX],row,col; //create the table for (row = 0; row <= ; row++) for (col = ; col <= ; ) = row * col; //print the table for (row = 1; row <= MAX - 1; row++) for ( ; ; col++) printf("%4d", ); printf("\n"); 4) Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin 20 times, and count the number of times each side of the coin appears. Print the results. (0 for tails and 1 for heads) (hint: lab3 task3) 5

5) Complete and run the following program. This program insert an element in an array void main() int arr[30], element, num, i, location; printf("\nenter no of elements :"); scanf("%d", &num); for (i = 0; i < num; i++) printf("arr[%d]: ",i); scanf(, ); //read values to the array printf("\nenter the element to be inserted :"); scanf("%d", &element); printf("\nenter the location: "); scanf("%d", &location); //Create space at the specified location for (i = num; i >= location; i--) arr[ ] = arr[ ]; //move array elements ; //increment num by one arr[location] = element; //Print out the result of insertion for (i = 0; i < num; i++) printf("%4d", ); //print array elements printf("\n"); 6

6) Write a c program to delete an element from specified location from array 7

7) Complete the following program below by filling in the blanks. #define MAXGRADES 5 // maximum number of grades // function protoype void main() int count; int i; float grade[ MAXGRADES ]; float sumofgrades = 0.0; // actual number of grades // loop counter // array of size MAXGRADES // sum of array elements // prompt the user for the number of scores (less than MAXSCORE) printf( "Enter the number of scores: "); scanf("%d",&count); i // read in the scores and store them in the array printf( "\nenter the scores: \n"); for (i = ; i <= ; i++) scanf("%f",& ); // read in grade array element sumofgrades+=grade[i]; printf("\n"); // compute the sumofgrades here // find and display the average of the scores printgrades(, ); // call function printgrades printf( "The grade average is %.2f\n\n\n",sumofgrades / count ); void printgrades(float num[], int size) printf( "The grades are:\n"); for (int i = 0; i <= ; i++) printf("%6.2f\n", ); printf("\n"); 8

8) Complete the following code. (Read comments for assistance.) #define MAX 10 void bubblesort(int array[], int size); // bubble sort function prototype void swap( ); // helper function prototype of bubblesort ; // print array function prototype void main() // Declare an array called value of size MAX with // elements: 20, 15, 18, 19, 22, 11, 9, 7, 10, 3 ; printf("array before sort: \n"); // Place print array function call here. ; // Place bubble sort function call here. ; printf("\n\narray after sort: \n"); // Place print array function call here. ; printf("\n\n\n"); void bubblesort(int array[], int size) for (int pass = 1; pass <= size - 1; pass++) for (int i = 0; i <= size - 2 ; i++) if (array[i] > array[i + 1]) swap(array[i], array[i + 1]); void swap ( ) int temp; temp = ; = ; = temp; void printarray( int array[], int size) for (int i = 0; i <= ; i++) printf("%4d", ) ; 9