UNIT - 3 Noornilo Nafees.

Slides:



Advertisements
Similar presentations
Sort the given string, without using string handling functions.
Advertisements

To remind us We finished the last day by introducing If statements Their structure was:::::::::
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.
More Questions 1) Write a C program to read a matrix A of size nXn and two arrays X and Y of size n and calculate XA-Y?
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
Write a C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Write a C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function.
Types of Operator Arithmetic operator Relational operator Logical operator Assignment operator Increment or decrement operator(unary) Bitwise operator.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
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.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
UNIT - 3 ARRAYS AND STRINGS. Array An Array is a collection of similar data items, that are stored under a common name. Types –One-Dimensional array –Two-Dimensional.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional Array Explain Arrays.
MAHENDRAN. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional.
UNIT - 5 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Lecture2.
Arithmetic Expressions
C ARRAYS.
Chapter 8 “Character Arrays and Strings” Prepared by: Prof. Ajay M. Patel CE, IDS, NU.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7 Arrays 1. Concept of arrays Array and pointers
CHP-2 ARRAYS.
C Scope Rules and Arrays
LESSON 3 IO, Variables and Operators
Lecture 8 String 1. Concept of strings String and pointers
MULTI-DIMENSIONAL ARRAY
Chapter 7: Array.
Module 2 Arrays and strings – example programs.
ARRAYS An array is a sequence of data item of homogeneous value(same type). Arrays are of two types: 1. One-dimensional arrays 2. Multi-Dimensional arrays.
Computer Science 210 Computer Organization
Visit for more Learning Resources
Formatted and Unformatted Input/Output Functions
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Computer Science 210 Computer Organization
CS111 Computer Programming
Chapter 5 POINTERs.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
FUNCTIONS AND POINTERS
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Chapter 2 Array and String Visit to more Learning Resources.
C Strings Prabhat Kumar Padhy
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Strings in C Array of characters is called a string.
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Course Outcomes of Programming In C (PIC) (17212, C203):
C Characters and Strings
Getting Started With Coding
Introduction to Problem Solving and Programming
Visit for more Learning Resources
Presentation transcript:

UNIT - 3 Noornilo Nafees

Array It is a collection of elements of same data type, that are referenced by a common name Types One-Dimensional array Two-Dimensional array Multi-Dimensional array Noornilo Nafees

One-Dimensional array Array Declaration Syntax: datatype array_name[size]; Example: int x[3]; x X[0] X[1] X[2] Noornilo Nafees

x X[0] X[1] X[2] Array initialization Syntax: data_type array_name[size]={variables}; Example: int x[3]={5,3,7}; x int a[5]; a[0]=10; a[1]=20; a[2]=30; a[3]=40; a[4]=50; X[0] X[1] X[2] 5 3 7 Int a[5]={10,20,30,40,50}; char a[6]=“Nafees”; Noornilo Nafees

//program to set values of array and display it #include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); a[0]=10; a[1]=20; a[2]=30; a[3]=40; a[4]=50; for(i=0;i<5;i++) printf("\nThe value in a[%d] is %d",i,a[i]); } getch(); OUTPUT The value in a[0] is 10 The value in a[1] is 20 The value in a[2] is 30 The value in a[3] is 40 The value in a[4] is 50 Noornilo Nafees

//program to get values of array from users and display it #include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } printf("\nThe value in a[%d] is %d",i,a[i]); getch(); OUTPUT Enter five values : 10 20 30 40 50 The value in a[0] is 10 The value in a[1] is 20 The value in a[2] is 30 The value in a[3] is 40 The value in a[4] is 50 Noornilo Nafees

//program to find maximum no in an array #include<stdio.h> #include<conio.h> void main() { int a[5],i,max; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } max=a[0]; for(i=1;i<5;i++) if(max<a[i]) max=a[i]; printf(“Maximum no is %d“,max); getch(); } OUTPUT Enter five values : 10 20 30 40 50 Maximum no is 50 Noornilo Nafees

//program to find sum of elements in an array #include<stdio.h> #include<conio.h> void main() { int a[5],i,sum=0; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } sum=sum+a[i]; printf(“The sum of elements in array is : %d”,sum); getch(); OUTPUT Enter five values : 1 2 3 4 5 The sum of elements in array is : 15 Noornilo Nafees

//program to sort number in ascending order #include<stdio.h> #include<conio.h> void main() { int a[5],i,j,t; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } for(j=i+1;j<5;j++) if(a[i]>a[j]) t=a[i]; a[i]=a[j]; a[j]=t; } printf(“Ascending order : \n”); for(i=0;i<5;i++) { printf(“%d\t”,a[i]); getch(); OUTPUT Enter five values : 40 10 30 50 20 Ascending order : 10 20 30 40 50 Noornilo Nafees

//program to search element in array(Linear Search) #include<stdio.h> #include<conio.h> void main() { int a[5]={10,20,30,40,50}; int key; clrscr(); printf("Enter search value:\n"); scanf("%d",&key); for(i=0;i<5;i++) if(key==a[i]) printf(“Value found”); break; } else { printf(“Value not found”); } getch(); OUTPUT Enter search value : 40 Value found Noornilo Nafees

Two-Dimensional array Array Declaration Syntax: data_type array_name[row_size] [col_size]; Example: int x[3][3]; Col 0 Col 1 Col 2 row 0 row 1 row 2 X[0][0] X[1][0] X[2][0] X[0][1] X[1][1] X[2][1] X[0][2] X[1][2] X[2][2] Noornilo Nafees

Array Initialization Syntax: data_type array_name[row_size] [col_size];={variables}; Example: int x[3][3]={1,50,2,75,8,4,9,33,77}; Col 0 Col 1 Col 2 row 0 row 1 row 2 1 50 2 75 8 4 9 33 77 Noornilo Nafees

//program to assign values to array and to display it #include<stdio.h> #include<conio.h> void main() { int a[3][3]={10,20,30,40,50,60,70,80,90}; int i,j; clrscr(); printf(“Values in array : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); } getch(); OUTPUT Values in array : a[0][0]=10 a[0][1]=20 a[0][2]=30 a[1][0]=40 a[1][1]=50 a[1][2]=60 a[2][0]=70 a[2][1]=80 a[2][2]=90 Noornilo Nafees

//program to assign values to array from user and to display it #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf(“Enter 9 values in array : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“Values in array : \n”); printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); getch(); } OUTPUT Enter 9 values in array : 10 20 30 40 50 60 70 80 90 Values in array : a[0][0]=10 a[0][1]=20 a[0][2]=30 a[1][0]=40 a[1][1]=50 a[1][2]=60 a[2][0]=70 a[2][1]=80 a[2][2]=90 Noornilo Nafees

//program to implement Matrix addition #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf(“Enter values of Matrix A : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“Enter values of Matrix B : \n"); scanf(“%d”,&b[i][j]); for(i=0;i<3;i++) { for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; } printf(“Added Matrix\n”); printf(“%d\t”,c[i][j]); printf(“\n”); getch(); Noornilo Nafees

//program to implement Matrix addition OUTPUT Enter values of Matix A : 1 2 3 4 5 6 7 8 9 Enter values of Matix B : OUTPUT Addded Matrix 4 6 10 12 14 16 18 Noornilo Nafees

//program to implement Matrix multiplication #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); printf(“Enter values of Matix A : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“Enter values of Matix B : \n"); scanf(“%d”,&b[i][j]); for(i=0;i<3;i++) { for(j=0;j<3;j++) c[i][j]=0; for(k=0;k<3;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf(“Multiplied Matrix\n”); printf(“%d\t”,c[i][j]); printf(“\n”); getch(); Noornilo Nafees

//program to implement Matrix OUTPUT Enter values of Matrix A : 1 Enter values of Matrix B : OUTPUT Multiplied Matrix 1 1 1 1 1 1 Noornilo Nafees

//program to find transpose of Matrix #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf(“Enter values in array : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“\nTranspose of given Matrix : \n”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(“%d\t”,i,j,a[j][i]); } printf(“\n”); } getch(); OUTPUT Enter values in array : 1 2 3 4 5 6 7 8 9 Transpose of given Matrix : 1 4 7 2 5 8 3 6 9 Noornilo Nafees

Strings Handling of Character Strings Syntax: char var_name[size]=“String”; Example char a[6]=“Cse”; #include<stdio.h> #include<conio.h> void main() { char a[6]; clrscr(); printf(“Enter a string : “); scanf(“%s”,a); printf(“Your String is : %s”,a); getch(); } OUTPUT Enter a string: cse Your String is : cse Noornilo Nafees

String Functions strlen() It is used to find the length of the string. syntax: strlen(string) strcpy() It is used to copy one string to another. strcpy(string1,string2) strcat() It is used to combine two strings. strcat(string1,string2) Noornilo Nafees

It is used to compare two strings. syntax: strcmp(string1,string2) Returns 0 if two strings are equal. strrev() It used to reverse a string. strrev(string) strlwr(), strupr() It used to change the case of a string. strlwr(string) strupr(string) Noornilo Nafees

#include<stdio.h> #include<conio.h> strlen() It is used to find the length of the string. syntax: strlen(string) #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="college"; int b; clrscr(); b=strlen(a); printf("\nThe length of the string is %d",b); getch(); } Output: The length of the string is 7 Noornilo Nafees

#include<stdio.h> #include<conio.h> strcpy() It is used to copy one string to another. syntax: strcpy(string1,string2) #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]=“Cse"; char b[]="Dept"; clrscr(); strcpy(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is Dept Noornilo Nafees

#include<stdio.h> #include<conio.h> strcat() It is used to combine two strings. syntax: strcat(string1,string2); #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]=“Cse"; char b[]="Dept"; clrscr(); strcat(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is CseDept Noornilo Nafees

#include<stdio.h> #include<conio.h> strcmp() It is used to compare two strings. syntax: strcmp(string1,string2) Returns 0 if two strings are equal. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[10]=“csedept"; Output: char b[10]=“cse"; Strings are not equal int i; clrscr(); i=strcmp(a,b); if(i==0) printf(“Strings are equal”); else printf("Strings are not equal”); getch(); } Noornilo Nafees

strrev() It used to reverse a string. syntax: strrev(string); #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="Dept"; clrscr(); printf("\nThe reversed string is %s",strrev(a)); getch(); } Output: The string is tpeD Noornilo Nafees

#include<stdio.h> #include<conio.h> strlwr(), strupr() It used to change the case of a string. syntax: strlwr(string); strupr(string); #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[20]=“csedept"; clrscr(); printf("\nThe string is :%s",a); strupr(a); printf("\nThe string after conversion to uppercase :%s",a); strlwr(a); printf("\nThe string after conversion to lowercase :%s",a); getch(); } Output The string is :csedept The string after conversion to uppercase :CSEDEPT The string after conversion to lowercase :csedept Noornilo Nafees

String Palindrome #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s1[15],s2[15]; printf("\nenter the string:"); scanf("%s",s1); strcpy(s2,s1); strrev(s1); Noornilo Nafees

The string is palindrome if(strcmp(s1,s2)==0) printf("\n The string is palindrome"); else printf("\n The string is not a palindrome"); getch(); } Output: enter the string: aba The string is palindrome Noornilo Nafees

Managing I/O Operations Noornilo Nafees

Input/Output Function Formatted Unformatted Input scanf() Output printf() Input getc() gets() getchar() getch() Output putc() puts() putchar() Noornilo Nafees

Formatted Input/Output C uses two functions for formatted input and output. Formatted input : reads formatted data from the keyboard. Formatted output : writes formatted data to the monitor. Noornilo Nafees

Formatted Input and Output Noornilo Nafees

Standard Output The standard output file is the monitor. Like the keyboard, it is a text file. When you need to display data that is not text, it must be converted into to the text before it is written to the screen. Noornilo Nafees

Format of printf Statement Noornilo Nafees

Formatted Input (scanf) The standard formatted input function in C is scanf (scan formatted). scanf consists of : a format string . an address list that identifies where data are to be placed in memory. scanf ( format string, address list ); (“%c….%d…..%f…..”, &a,….&i,…..,&x…..) Noornilo Nafees

Format of scanf Statement Noornilo Nafees

getchar() Example #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char x; printf("enter the character:"); x=getchar(); Noornilo Nafees

if(islower(x)) putchar(toupper(x)); else putchar(tolower(x)); getch(); } Output: enter the character : A a Noornilo Nafees

getc Example #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char x; printf("enter the character:"); x=getc(stdin); Noornilo Nafees

if(islower(x)) putc(toupper(x),stdout); else putc(tolower(x),stdout); getch(); } Output: enter the character:a A Noornilo Nafees

gets() Example #include <stdio.h> #include<conio.h> void main() { char c[80]; clrscr(); printf("Input a string:"); gets(c); Noornilo Nafees

printf("The string is:"); puts(c); getch(); } Output: Input a string:qwerty The string is:qwerty Noornilo Nafees

getch() Example #include <stdio.h> #include <conio.h> void main() { char c; clrscr(); printf("\nInput a character:"); c = getch(); Noornilo Nafees

printf("\nCharacter is:"); putch(c); getch(); } Output: Input a character : N Character : N Noornilo Nafees