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.

Slides:



Advertisements
Similar presentations
Operations on Arrays. Operation on Array Data Structures  Traversal  Selection  Searching  Insertion  Deletion  Sorting.
Advertisements

ICS103: Programming in C Searching, Sorting, 2D Arrays
Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
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?
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Array.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
PRESENTATION ON SEARCHING SEARCHING Searching is the method to find element from the list of the elements. If element is found then it.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
UNIT II. -set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
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 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
GE 211 Dr. Ahmed Telba. TO GIVE integer number #include using namespace std; // function declaration int main () { int i,k; float y,m; cin>>y ; i=y;//
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear.
ICS103: Programming in C Searching, Sorting, 2D Arrays Muhamed F. Mudawar.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
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.
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.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Rray Programs. Insert and element in the given position of the Array rray.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
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.
Computer Programming for Engineers
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
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 C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
1 More on Arrays. 2 Review: Passing Arrays to Functions Passing an array: what goes on in the memory? Why isn’t it the same as when passing primitives?
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
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.
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.
Lecture 7 Arrays 1. Concept of arrays Array and pointers
MULTI-DIMENSIONAL ARRAY
Week 4 – Repetition Structures / Loops
Array 9/8/2018.
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.
CHAPTER 7 RECURSIVE FUNCTION
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Lecture 10 Arrays.
Chapter 7 Arrays PROGRAMMING IN ANSI C.
UNIT - 3 Noornilo Nafees.
Introduction to Problem Solving and Programming
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS
ICS103: Programming in C Searching, Sorting, 2D Arrays
Visit for more Learning Resources
Presentation transcript:

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. #include <stdio.h> float average(float a[]); int main(){ float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18}; avg=average(c); /* Only name of array is passed as argument. */ printf("Average age=%.2f",avg); return 0; } float average(float a[]){ int i; float avg, sum=0.0; for(i=0;i<6;++i){ sum+=a[i]; avg =(sum/6); return avg; Output Average age=27.08

Example to pass two-dimensional arrays to function #include void Function(int c[2][2]); int main() { int c[2][2],i,j; printf("Enter 4 numbers:\n"); for(i=0;i<2;++i) for(j=0;j<2;++j) scanf("%d",&c[i][j]); } Function(c); /* passing multi-dimensional array to function */ return 0; void Function(int c[2][2]) /* Instead to above line, void Function(int c[][2]){ is also valid */ int i,j; printf("Displaying:\n"); printf("%d\n",c[i][j]);

Output Enter 4 numbers: 2 3 4 5 Displaying:

Write a C program to find sum of two matrix of order 2 Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix are entered by user. #include <stdio.h> int main() { float a[2][2], b[2][2], c[2][2]; int i,j; printf("Enter the elements of 1st matrix\n"); /* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/ for(i=0;i<2;++i) for(j=0;j<2;++j) printf("Enter a%d%d: ",i+1,j+1); scanf("%f",&a[i][j]); } printf("Enter the elements of 2nd matrix\n"); for(i=0;i<2;++i) for(j=0;j<2;++j)

printf("Enter b%d%d: ",i+1,j+1); scanf("%f",&b[i][j]); } { printf("Enter b%d%d: ",i+1,j+1); scanf("%f",&b[i][j]); } for(i=0;i<2;++i) for(j=0;j<2;++j) /* Writing the elements of multidimensional array using loop. */ c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ printf("\nSum Of Matrix:"); printf("%.1f\t",c[i][j]); if(j==1) /* To display matrix sum in order. */ printf("\n"); return 0;

OUTPUT Enter the elements of 1st matrix Enter a11: 2; Enter a12: 0.5; Enter a21: -1.1; Enter a22: 2; Enter the elements of 2nd matrix Enter b11: 0.2; Enter b12: 0; Enter b21: 0.23; Enter b22: 23; Sum Of Matrix: 2.2 0.5 -0.9 25.0

#include<stdio.h> int fact(int); int main(){ int num,f; Factorial program by recursion in c Factorial program in c using recursion C program to calculate factorial using recursion Recursive function for factorial in c #include<stdio.h> int fact(int); int main(){   int num,f; printf("\nEnter a number: ");   scanf("%d",&num);   f=fact(num);   printf("\nFactorial of %d is: %d",num,f);   return 0; } int fact(int n){    if(n==1)        return 1;    else        return(n*fact(n-1));  }

Matrix multiplication #include<stdio.h> int main(){   int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;   printf("\nEnter the row and column of first matrix");   scanf("%d %d",&m,&n);   printf("\nEnter the row and column of second matrix");   scanf("%d %d",&o,&p);   if(n!=o){       printf("Matrix mutiplication is not possible");       printf("\nColumn of first matrix must be same as row of second matrix");   }   else{       printf("\nEnter the First matrix->");       for(i=0;i<m;i++)       for(j=0;j<n;j++)            scanf("%d",&a[i][j]);       printf("\nEnter the Second matrix->");       for(i=0;i<o;i++)       for(j=0;j<p;j++)            scanf("%d",&b[i][j]);       printf("\nThe First matrix is\n");       for(i=0;i<m;i++){       printf("\n");       for(j=0;j<n;j++){            printf("%d\t",a[i][j]);       }       }

printf("\nThe Second matrix is\n");       for(i=0;i<o;i++){       printf("\n");       for(j=0;j<p;j++){            printf("%d\t",b[i][j]);       }              }       for(i=0;i<m;i++)       for(j=0;j<p;j++)            c[i][j]=0;       for(i=0;i<m;i++){ //row of first matrix       for(j=0;j<p;j++){  //column of second matrix            sum=0;            for(k=0;k<n;k++)                sum=sum+a[i][k]*b[k][j];            c[i][j]=sum;       }   }   printf("\nThe multiplication of two matrix is\n");   for(i=0;i<m;i++){       printf("\n");       for(j=0;j<p;j++){            printf("%d\t",c[i][j]);       }   return 0; }

#include<stdio.h> int main(){ int a[10],i,n,m,c=0,l,u,mid; Write a simple code for binary search in c programming language Wap a c program to search an element in an array using binary search #include<stdio.h> int main(){     int a[10],i,n,m,c=0,l,u,mid;     printf("Enter the size of an array: ");     scanf("%d",&n);     printf("Enter the elements in ascending order: ");     for(i=0;i<n;i++){          scanf("%d",&a[i]);     }     printf("Enter the number to be search: ");     scanf("%d",&m); l=0,u=n-1;     while(l<=u){          mid=(l+u)/2;          if(m==a[mid]){              c=1;              break;          }    

Enter the size of an array: 5 else if(m<a[mid]){              u=mid-1;          }          else              l=mid+1;     }     if(c==0)          printf("The number is not found.");     else          printf("The number is found.");     return 0; } OUTPUT: Enter the size of an array: 5 Enter the elements in ascending order: 4 7 8 11 21 Enter the number to be search: 11 The number is found.

Write a C program which will accept multiple strings the user and will sort them in ascending order. #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char *str[5], *temp; int i, j, n; printf("\nHow many names do you want to have?"); scanf("%d", &n); for (i = 0; i < n; i++) { printf("\nEnter the name %d: ", i); flushall(); gets(str[i]); }

for (i = 0; i < n; i++) {       for (j = 0; j < n - 1; j++) {          if (strcmp(str[j], str[j + 1]) > 0) {             strcpy(temp, str[j]);             strcpy(str[j], str[j + 1]);             strcpy(str[j + 1], temp);          }       }    }     flushall();     printf("\nSorted List : ");    for (i = 0; i < n; i++)       puts(str[i]);    return (0); }

OUTPUT How many names do you want to have? 4 Enter the name 0: pri Enter the name 1: prt Enter the name 2: prq Enter the name 3: pra   Sorted List : pra pri prq prt