EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays and Strings.
What is shape function ? shape function is a function that will give the displacements inside an element if its displacement at all the node locations.
Week 8 Arrays Part 2 String & Pointer
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Introduction to Programming with C++ Fourth Edition
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
Nested LOOPS.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
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 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Arrays. Arrays are objects that help us organize large amounts of information.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
ARRAYS.
multi-dimensional arrays
© 2016 Pearson Education, Ltd. All rights reserved.
Multi-dimensional Array
FUNCTIONS WITH ARGUMENTS
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions with arrays.
Arrays Arrays A few types Structures of related data items
Functions Extra Examples.
Arrays.
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Presentation transcript:

EXAMPLE

Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100. The subscripts are to be stored in another array Y of the same size. The array X is filled by the user. The array Y is initialized to -1.

Dr. Soha S. Zaghloul3 #include int main (void) { int X[100], int Y[100], sub, subY; // fill and initialize the arrays for (sub = 0; sub < 100; sub++) { printf (“Enter elements of X> “); scanf (“%d”, &X[sub]); Y[sub] = -1; } // end for // get the subscripts of multiples of 7 in X, and store them in Y subY = 0; //use another subscript subY for Y and initializes subY for (sub = 0; sub < 100; sub++) { if (X[sub]%7 == 0) { Y[subY] = sub; subY++; } //end if } // end for } // end main

Dr. Soha S. Zaghloul4 #include int main (void) { int X[100], int Y[100], sub, subY; subY = 0;// intialize the subscript of Y for (sub = 0; sub < 100; sub++) { printf (“Enter elements of X> “); scanf (“%d”, &X[sub]); Y[sub] = -1; if (X[sub]%7 == 0) { Y[subY] = sub; subY++; } //end if } // end for } // end main

Dr. Soha S. Zaghloul5 Write a complete modular program that contains the following functions: 1.Function InitArray that initializes an array with the same value for all elements. The value is of type int. InitArray accepts an array of type integer, its size, and the initial value. 2.Function FillArray that fills an array with values from the user. FillArray accepts an array of type integer and its size. 3.Function Mod7occ (Mod 7 occurrences) that searches for all the elements that are multiple of 7 in an array. The subscripts are to be stored in another array of the same size. Mod7occ accepts two arrays of type integer and of the same size. One of the arrays is accepted as an input parameter.

Dr. Soha S. Zaghloul6 void FillArray (int list[], int size) { int i; for (i = 0; i < size; i++) { printf (“Enter the next array element> “); scanf (“%d”, &list[i]); } // end for } // end FillArray

Dr. Soha S. Zaghloul7 void InitArray (int list[], int size, int value) { int i; for (i = 0; i < size; i++) list[i] = value; } // end InitArray

Dr. Soha S. Zaghloul8 void Mod7occ (const int list1[], int list2[]) { int i, j;// i is used as a subscript to list 1, j is used as a subscript to list2 for (i = 0; i < size; i++) if (list1[i] % 7 == 0) { list2[j] = i; j++; } // end for } // end Mod7occ

Dr. Soha S. Zaghloul9 void Modnocc (const int list1[], int list2[], int n) { int i, j;// i is used as a subscript to list 1, j is used as a subscript to list2 for (i = 0; i < size; i++) if (list1[i] % n == 0) { list2[j] = i; j++; } // end for } // end Modnocc Mod7occ may be generalized to search for the multiples of any number n. n will be specified by the user. We will call this Modnocc.

Dr. Soha S. Zaghloul10 #include #defineSIZE100 // Prototypes void FillArray (int list[], int size); void InitArray (int list[], int size, int value); void Modnocc (const int list1[], int list2[], int n); // main program int main (void) { int X[SIZE], Y[SIZE]; int init = -1;// value used to initialize Y int num = 7;// search for mod num. You can also get the value through scanf int sub; // used as the subscript to print Y // calling functions FillArray (X, SIZE); InitArray (Y, SIZE, init); Modnocc(X, Y, num); // printing Y for (sub = 0; sub < SIZE; sub++) printf (“%d \n”, Y[sub]); } // end main // Functions Definition void FillArray (int list[], int size) {--- } //end of FillArray void InitArray (int list[], int size, int value) {--- } // end of InitArray void Modnocc (const int list1[], int list2[], int n) { --- } // end of Modnocc

Dr. Soha S. Zaghloul11 Write a function MultiScalar to multiply a two-dimensional array by a scalar. The array and the scalar are of type integer. void MultiScalar (int rows, int cols, int list[rows][cols], int scalar) { int i;// i is used as a counter to the rows int j; // j is used as a counter to the columns for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) list[i][j] = list[i][j] * scalar; } // end MultiScalar Note that: 1.we must identify the number of rows and columns of a two- dimensional array in the function header. 2.The number of rows and columns are defined in the function header before defining the two-dimensional array. 3.An element of a two-dimensional array is referred to by list[i][j] not list[i,j]

Dr. Soha S. Zaghloul12 Write a function DisplayMat to display a two-dimensional matrix. void DisplayMat (int rows, int cols, const int list[rows][cols]) { int i;// i is used as a counter to the rows int j; // j is used as a counter to the columns for (i = 0; i < rows; i++)// loop through the rows for (j = 0; j < cols; j++) // loop through the columns { printf (“%d\t”, list[i][j]);// leave a tab between elements in the same row printf (“\n”); // start a new line for a new row } // end DisplayMat

Dr. Soha S. Zaghloul13 Write a function MatProduct to get the product of all elements of a two- dimensional array. The array is of type integer. Write a function MatManipulate that manipulates a two-dimensional array. In addition to the input array, the function accepts an output array to store the results, an integer scalar, a character operation that specifies the arithmetic operation to be performed between the scalar and the array.