What we have learned so far

Similar documents
Problem: Read 10 numbers from the keyboard and store them

AMCAT Automata Coding Sample Questions And Answers

BRANCHING if-else statements

Introduction to C Programming

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

Decision Making -Branching. Class Incharge: S. Sasirekha

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

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

Unit 3 Decision making, Looping and Arrays

Subject: Fundamental of Computer Programming 2068

Worksheet 4 Basic Input functions and Mathematical Operators

Chapter 4 C Program Control

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

DECISION CONTROL AND LOOPING STATEMENTS

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CSC 270 Survey of Programming Languages

Week 4 Selection Structures. UniMAP Sem II-11/12 DKT121 Basic Computer Programming 1

Detail from C Propaganda Poster // Reddit user TheBall BBM 101. Introduction to Programming I. Lecture #11 C Iterations, Functions, Multi-D Arrays

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

1. The keyword main in C language is used for

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Chapter 2 - Control Structures

Computer Programming: Skills & Concepts (CP) Variables and ints

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Chapter 4 C Program Control

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Programming Language A

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

Structured programming

ECE 122. Engineering Problem Solving with Java

Chapter 3 Structured Program Development

Decision Making and Loops

provided. Use any other space for rough work. The question paper has total 8 pages. Rough Work

Control Structure: Selection

1.4 Control Structures: Selection. Department of CSE

BİL200 TUTORIAL-EXERCISES Objective:

The Hyderabad Public School, Begumpet, Hyderabad, A.P

ESC101N: Fundamentals of Computing End-sem st semester

Introduction to C Final Review Chapters 1-6 & 13

One Dimension Arrays 1

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

EENG 212 Lab 2. Recursive Functions

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

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

Additional Questions

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

More examples for Control statements

Principles of Programming. Chapter 6: Arrays

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

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

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

C Programming Review CSC 4320/6320

EL2310 Scientific Programming

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

C-Programming. Claude Fuhrer 5 août 2017

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

Technical Questions. Q 1) What are the key features in C programming language?

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

CGS 3460 Summer 07 Midterm Exam

Programming for Engineers Arrays

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

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

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

Arithmetic Expressions in C

Introduction. C provides two styles of flow control:

Government Polytechnic Muzaffarpur.

C Programming Multiple. Choice

printf( Please enter another number: ); scanf( %d, &num2);

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

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

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

COMP 2001/2401 Test #1 [out of 80 marks]

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

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

Unit 7. Functions. Need of User Defined Functions

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

Ex: If you use a program to record sales, you will want to remember data:

Lecture 2: C Programming Basic

C library = Header files + Reserved words + main method

بسم اهلل الرمحن الرحيم

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

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

int marks[10]; // fixed size and fixed address No change in Memory address.

Subject: PIC Chapter 2.

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

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

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

H192 Midterm 1 Review. Tom Zajdel

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

Transcription:

What we have learned so far Straight forward statements Conditional statements (branching) Repeated statements (loop)

Arrays One variable many data

Problem: Read 10 numbers from the keyboard and store them

100 Problem: Read 10 numbers from the keyboard and store them // solution #1 int a, b, c, d, e, f, g, h, i, j; printf( Enter a number: ); scanf( %d, &a); printf( Enter a number: ); scanf( %d, &b); // printf( Enter a number: ); scanf( %d, &j);

Arrays An array is an ordered list of data values of similar kind The entire array has a single name Each value has a numeric index 0 1 2 3 4 5 6 7 8 9 a 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9

An array with 8 elements of type double

Arrays The values held in an array are called array elements An array stores multiple values of the same type the element type The element type can be a primitive type Therefore, we can create an array of int, an array of float, an array of double or an array of character.

Declaring Arrays data_type array_name[size]; For example: int a[10]; a is an array of 10 integers. float prices[3]; prices is an array of 3 floats. char c[6]; c is an array of 6 characters.

How to assign values? There are 3 ways.

How to assign values? First way It is possible to initialize an array when it is declared: float prices[3] = {1.0, 2.1, 2.0};

How to assign values? First way (Continue) Declaring an array of characters of size 3: char letters[3] = { a, b, c }; Or we can skip the 3 and leave it to the compiler to estimate the size of the array: char letters[] = { a, b, c };

How to assign values? Second way: Use assignment operator int a[6]; a[0]=3; a[1]=6;

How to assign values? Third way: Use scanf to input in the array: int a[6]; Array index could be constant, integer variable or expressions that generate integers scanf( %d, &a[0]); scanf( %d, &a[1]);.... scanf( %d, &a[5]); int a[6]; for(i= 0; i<6; i++){ scanf( %d, &a[i]); }

Arrays: Some examples Example 1: Take N students marks as input and store them in an array. Find average mark. N will also be input. (Do it for 5 students first) Example 2: Take N students marks as input and store them in an array. Find grade of each student. Example 3: Take N numbers as input and store them in an array. Print all odd numbers in the array. Example 4: Take N students marks as input and store them in an array. Find highest/lowest mark. Example 5: Take N numbers as input and store them in an array. Shift all elements of the array one place towards left. The first element will go to the last place.

Problem: Find the minimum number in an array of unsorted integers find_minimum.c

#include <stdio.h> #include <stdlib.h> find_minimum.c #define N 12 int main() { int a[n] = { 14, 21, 36, 14, 12, 9, 8, 22, 7, 81, 77, 10}; int i; // Find The Minimum Element int min=a[0]; // pick the first number as the current minimum for(i=1; i< N; i++) { if(a[i] < min) { min=a[i]; } } } printf("the minumum value in the array is %d.\n\n", min);

Problem: Find the minimum number (and its index) in an array of unsorted integers find_minimum_and_index.c

find_minimum_and_index.c #include <stdio.h> #include <stdlib.h> #define N 12 int main() { int a[n] = { 14, 21, 36, 14, 12, 9, 8, 22, 7, 81, 77, 10}; int i, min; // Find The Minimum Element and it index min= a[0]; // initial guess: a[0] is the minimum value int idx=0; // initial guess: the minimum value is at index 0 } for(i=0; i< N; i++) { if(a[i] < min) { min=a[i]; idx=i; } } printf("the minumum value in the array is %d.\n\n", min); printf("it is located at index: %d \n\n", idx);

Search

The most basic Linear Search Very easy to implement The array DOESN T have to be sorted All array elements must be visited if the search fails Could be very slow

Example: Successful Linear Search Example: Successful Linear Search

Example: Failed Linear Search

Problem: Find the index of a number in an unsorted array of integers linear_search.c

Linear_Search.c #include <stdio.h> #include <stdlib.h> #define N 12 int main() { int a[n] = { 4, 21, 36, 14, 62, 91, 8, 22, 7, 81, 77, 10}; int i; int target, i, idx; printf("what do you want to search?"); scanf("%d", &target); idx = -1; for(i=0; i< N; i++) } { if(a[i] == target) { idx=i; break; } } if(idx == -1) printf("target not found.\n\n"); else printf("target found at index: %d \n\n", idx);

Linear Search in a Sorted Array Target 19 21 > 19

Problem: Find the index of a number in a sorted array of integers LinearSearch_InSortedArray.c

LinearSearch_InSortedArray.c #include <stdio.h> #include <stdlib.h> #define N 12 int main() { int a[n]= { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91}; int target, i, idx; printf("what do you want to search?"); scanf("%d", &target); idx = -1; for(i=0; i< N; i++) { if(a[i] == target) { idx=i; break; } else if(a[i]>target) break; // we can stop here } if(idx == -1) printf("target not found.\n\n"); else printf("target found at index: %d. \n\n", idx); }

Analysis If the list is unsorted we have to search all numbers before we declare that the target is not present in the array. Because the list is sorted we can stop as soon as we reach a number that is greater than our target Can we do even better?

Collecting statistics in an array Take N students marks as input and store them in array. Find and print how many of them got A, how many of them got B, C, D and F grades respectively. The grade chart is provided below: Marks Grade ===== ===== 90 100 A 80 89 B 70 79 C 60 69 D <60 F

Collecting statistics in an array #include <stdio.h> #define N 12 int main() { int a[n]= { 40, 72, 86, 10, 94, 91, 56, 36, 62, 77, 81, 91}; int acount, bcount, ccount, dcount, fcount; acount = bcount = ccount = dcount = fcount = 0; for(i=0; i< N; i++) { if(a[i] >= 90) else if(a[i] >= 80) else if(a[i] >= 70) else if(a[i] >= 60) else acount++; bcount++; ccount++; dcount++; fcount++; } } printf( Number of students getting A %d \n, acount); printf( Number of students getting B %d \n, bcount); printf( Number of students getting C %d \n, ccount); printf( Number of students getting D %d \n, dcount); printf( Number of students getting F %d \n, fcount);

Questions?