Arrays and Strings. Arash Rafiey. September 12, 2017

Similar documents
Functions. Arash Rafiey. September 26, 2017

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

Principles of Programming. Chapter 6: Arrays

Introduction to Scientific Computing and Problem Solving

Classification s of Data Structures

Array Initialization

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

COMPUTER APPLICATION

LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

CS16 Exam #1 7/17/ Minutes 100 Points total

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Lesson 7. Reading and Writing a.k.a. Input and Output

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

ARRAYS(II Unit Part II)

ONE DIMENSIONAL ARRAYS

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

LECTURE 17. Array Searching and Sorting

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

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

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

MTH 307/417/515 Final Exam Solutions

ESC101N Fundamentals of Computing

Computer Science & Engineering 150A Problem Solving Using Computers

LAB 5: REPETITION STRUCTURE(LOOP)

AMCAT Automata Coding Sample Questions And Answers

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

Basic and Practice in Programming Lab7

How to declare an array in C?

Introduction to C An overview of the programming language C, syntax, data types and input/output

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

Arrays. Eng. Mohammed Abdualal

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

C: How to Program. Week /Apr/23

Procedural Programming

Lab 3. Pointers Programming Lab (Using C) XU Silei

Write a C program using arrays and structure

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Programming for Engineers Arrays

KENDRIYA VIDYALYA CLRI CHENNAI AUTUMN BREAK HOLIDAY HW MARKS QUESTIONS : DATA STRUCTURE

Arrays and Applications

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Chapter 9 Introduction to Arrays. Fundamentals of Java

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

Review of Important Topics in CS1600. Functions Arrays C-strings

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Array. Arrays. Declaring Arrays. Using Arrays

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

One Dimension Arrays 1

LAB 5: REPETITION STRUCTURE(LOOP)

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Introduction to Computer Science Midterm 3 Fall, Points

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

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

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.

Why arrays? To group distinct variables of the same type under a single name.

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

C Programming Language

Lecture 04 FUNCTIONS AND ARRAYS

APSC 160 Review Part 2

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Module 6: Array in C

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

For instance, we can declare an array of five ints like this: int numbers[5];

Procedural programming with C

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

ECE264 Fall 2013 Exam 1, September 24, 2013

Questions Bank. 14) State any four advantages of using flow-chart

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Decision Making and Loops

Computers Programming Course 11. Iulian Năstac

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Chapter 6: Using Arrays

C programming Lecture 2. Marina Krstic Marinkovic School of Mathematics Trinity College Dublin

Lecture06: Pointers 4/1/2013

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

What will happen if the user presses <enter> after every input? Will all work like below? Enter the values of x, y, z: Result: The sum is 17.

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Subject: Fundamental of Computer Programming 2068

Arrays. Storing Multiple Values

today cs3157-fall2002-sklar-lect05 1

EM108 Software Development for Engineers

#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.

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

tablica: array: dane_liczbowe

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Single Dimension Arrays

Transcription:

September 12, 2017

Arrays Array is a collection of variables with the same data type.

Arrays Array is a collection of variables with the same data type. Instead of declaring individual variables, such as number0, number1,..., and number99, one array variable such as numbers can be used. And numbers[0], numbers[1], and..., numbers[99] represent individual variables.

Arrays Array is a collection of variables with the same data type. Instead of declaring individual variables, such as number0, number1,..., and number99, one array variable such as numbers can be used. And numbers[0], numbers[1], and..., numbers[99] represent individual variables. All arrays consist of contiguous memory locations.

Declaration Syntax: type arrayname [number of elements in array]; Here type can be any data type such as int or char.

Declaration Syntax: type arrayname [number of elements in array]; Here type can be any data type such as int or char. For example: int arr[20]; Is an integer array of size 20.

Multi-dimensional arrays C supports multi-dimensional arrays.the simplest form of the multidimensional array is the two-dimensional array.

Multi-dimensional arrays C supports multi-dimensional arrays.the simplest form of the multidimensional array is the two-dimensional array. Syntax for two-dimensional array: type arrayname [ x ][ y ]; Here x is the number of rows and y is the number of columns For example: int two dimensional array[3][2];

Initializing an array An array can be initialized in many ways. Initializing each element separately: int arr[10]; int i = 0; for(i=0;i< sizeof(arr);i++) { arr[i] = i; // Initializing each element separately }

Initializing an array Initializing array at the time of declaration. int arr[ ] = { 1, 2, 3 }; Since we are initializing at the time of declaration so there is no need to mention any value in the subscripts [ ]. The size will automatically be calculated from the number of values. In this case, the size will be 3.

Initializing an array (string) Initializing array with a string (Method 1): The string in C is actually a one-dimensional array of characters which is terminated by a null character \0. Since strings are nothing but a series of characters so the array containing a string will be containing characters. char arr[ ] = { c, o, d, e, \0 }; The null byte is required as a terminating byte when string is read as a whole.

Initializing an array (string) Initializing array with a string (Method 2): char arr[ ] = code ; Here we can specify the whole string using double coats.

char test-string[20]; printf( enter a string \n ); scanf( %s, &test-string); // here s is for string int j=0; while (test-string[j]!= \0 ) { printf( %c,test-string[j]); j++; } printf( \n ); // print a new line

char test-string[20]; printf( enter a string \n ); scanf( %s, &test-string); // here s is for string int j=0; while (test-string[j]!= \0 ) { printf( %c,test-string[j]); j++; } printf( \n ); // print a new line char chr1= a ; printf( %d \n, chr1); // it prints out 97. ASCII code for z is 122

char test-string[20]; printf( enter a string \n ); scanf( %s, &test-string); // here s is for string int j=0; while (test-string[j]!= \0 ) { printf( %c,test-string[j]); j++; } printf( \n ); // print a new line char chr1= a ; printf( %d \n, chr1); // it prints out 97. ASCII code for z is 122 chr1= A ; printf( %d \n, chr1); // it prints out 65. ASCII code for Z is 90

Accessing values in an array The first element of array always has index 0. int arr[10]; int i = 0; for(i=0;i<sizeof(arr);i++) { arr[i] = i; // Initializing each element separately } int j = arr[5]; // Accessing the 6th element of integer array arr and assigning its value to integer j.

Example 1 Searching for an element in an array # include <stdio.h> void main() { int i, find, n; printf( Enter number of elements in array \n ); scanf( %d,&n); int arr[n]; printf( Enter the elements of the array \n ); for (i = 0; i<n; i++) scanf( %d, &arr[i]);

Example 1 Searching for an element in an array printf( Enter the search element ); scanf( %d,&find); for (i = 0; i<n; i++){ if(arr[i]==find){ printf( Element is present at position %d,i+1); break; } } if(i==n) printf( Element not found );

Sorting an Array # include <stdio.h> void main() { int i, n, smallest, index; printf( Enter number of elements in array \n ); scanf( %d,&n); int arr[n]; printf( Enter the elements of the array \n ); for (i = 0; i<n; i++) scanf( %d, &arr[i]);

Sorting an array in ascending order for (i = 0; i<n; i++){ smallest=arr[i]; } for (j = i + 1; j<n; j++) if (samllest > arr[j]){ smallest= arr[j]; index = j; } arr[index]= arr[i]; arr[i]=smallest; printf( The numbers in ascending order are: \n ); for (i = 0; i<n; i++) } printf( %d \n, arr[i]);

Tutorial for Thursday September 14 Question (1) Write a program that reads n integer numbers from the user and prints them in the reverse order. Your program should read n from the user first. Then ask the user to enter n integer numbers and print them in reverse order.

Tutorial for Thursday September 14 Question (1) Write a program that reads n integer numbers from the user and prints them in the reverse order. Your program should read n from the user first. Then ask the user to enter n integer numbers and print them in reverse order. Hint : You need to use array. Declare an array of size n. Read n numbers from input. Place them in the array. Read the array from index n 1 to 0 and print the elements of the array.

Question (2) We say a list of n integer numbers is 100-good if the difference between the smallest element and the largest element is at most 100. Write a program that reads a list of n integer numbers from the user and decides whether it is 100-good or not. Your program should read n from the user first. Then ask the user to enter n integer numbers. If it is not 100-good then your program should print out NO otherwise it should print the difference between the largest and smallest number in the list.

Question (2) We say a list of n integer numbers is 100-good if the difference between the smallest element and the largest element is at most 100. Write a program that reads a list of n integer numbers from the user and decides whether it is 100-good or not. Your program should read n from the user first. Then ask the user to enter n integer numbers. If it is not 100-good then your program should print out NO otherwise it should print the difference between the largest and smallest number in the list. Hint : You need to use array. Declare an array of size n. Read n numbers from input. Place them in the array. Take two dummy variables x, y (x for the smallest, y for the largest element). As you read the current value of the array compare it with x, y and update x, y accordingly. At the end check whether y x 100. If yes then print out y x. Otherwise print NO.

Question (3) Write a program that reads n numbers from the user and finds their median. Your program should read n from the user first. Then ask the user to enter n integer numbers. Example : The median of 1, 7, 3, 5, 6, 11, 10 is 6 Example : The median of 2, 7, 3, 3, 9, 6 is (3 + 6)/2

Question (3) Write a program that reads n numbers from the user and finds their median. Your program should read n from the user first. Then ask the user to enter n integer numbers. Example : The median of 1, 7, 3, 5, 6, 11, 10 is 6 Example : The median of 2, 7, 3, 3, 9, 6 is (3 + 6)/2 Hint : You need to sort the array and see the definition of the median.

Question (4) Write a program that converts all the temperature values from 0 F to 100 F (Fahrenheit) to Celsius.