Note on Array with Function

  • Note
  • Things to remember

The array can be passed as arguments to functions. An array can be passed as an argument from one function to another. Let a function sum computes the sum of the array elements

a[0], a[1],………..a[n]

Two parameters are required an array parameter a to catch the array passed and a parameter n to catch the index of the last item in the array to be summed. If we assume that the array is an array of ints and that the index n is of type int, the parameters in sum can be described as:

int sum(int a[], int n)

The parameter declaration for the array includes square brackets to signal the functions sum that a is an array name and not the name of an ordinary parameter.

# Write a program to demonstrate the array passing within the function.

#include <stdio.h>

#include <math.h>

#include <Conio.h>

void check (int n);

void main ()

{

int num [5];

clrscr ();

printf(“\n Example of passing individual elements to the function “);

for (int i=1; i<5; i++);

{

printf(“Enter a number:\n”);

scanf(“%d”, &num[i]);

}

for int i = 1; i<5; i++)

{

/*passing individual array */

check (num[j]);

}

}

/*function definition */

void check(int n)

{

if (n%2 ==0)

printf(“\n The number %d is even number”, n);

else printf(“\n The number %d is not even number”,n);

}

Using Address Operator

The base address of array elements can be passed to the called function using address operator &. After the base address is passed to the called function, pointer variables can access other elements of an array.

Passing array elements with an address:

  • Only the name of the array is as an argument.
  • It passes an address of the first element called base address to the called function and can be accessed to other elements by using pointer variables.

Array with Pointers

A pointer is a variable that can hold the address of another variable.

# Write a program to pass array of elements to the function sort() and print unsorted data before function call and sorted data after function call.

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <stdlib.h>

void sort (int *, int); /*function declaration */

void main ()

{

int num [] = {4,2,73,9,7};

clrscr ();

printf(“\n Printing array elements before function call”);

for (int i=0; i<5; i++)

{

printf(“\n Address = %u Value num [%d] = %d”, &num [i], num[i], i);

}

}

/* function definition */

void sort (int *n, int j)

{

int i, k;

for (i=0; i<j; i++)

{

int temp=0;

for (k=0; k<j-1; k++);

{

if(n[k]>n [k+1])

{

temp = n[k]

n[k]=n[k+1];

n[k+1]=temp;

}

}

}

}

# Write a program to find the greatest number among the given numbers.

#include <stdio.h>

#include <conio.h>

/* function declaration */

void greatest (int *, int);

void main ()

{

int num[]={4,2,72,9,5,7,90,88,34,10};

clrscr ();

/* greatest function call, first element address & find values */

greatest (&num [0], 9);

}

void greatest (int *n, int j)

{

int m=0, i;

for (i=0; i<j; i++)

{

if (*n>m)

{

m = *n; /* find the greatest no */

}

*n++; /* access next element with pointer */

}

printf(“\n Greatest num is %d”,m);

}

Arrays of Strings

A string is the sequence of characters placed one after the other terminated with a null character’\0’. Hence if you are entering a certain number of characters, the total characters in the array would be the sum of the characters plus one. Each character within the string will be stored within one element of the array.

It is true that some characters within a string can be processed individually (e.g. Piglatin). However, most of the strings are processed as complete entities. The strings are processed using string manipulating functions.

C compilers include library functions that allow strings to be compared, copied or concatenated. Other library functions permit operations on individual characters within the string. They allow individual characters to be located within the strings.

# Write a program to accept a character form the keyboard and print.

/* test of getchar () function */

#include <stdio.h>

main ()

{

char c;

printf(“\n Enter a character:”);

c = getchar ();

printf(“\n The character input is:”);

putchar (c);

}

# Write a program to convert lowercase character into uppercase character.

#include <stdio.h>

char lowertoupper (char c1)

{

char c2;

c2=(c1)=’a’ && c1<=’z’) ? (‘A’ + c1 – ‘a’) : c1;

return (c2);

}

main ()

{

char lower, upper;

printf(“Please enter a lowercase character:”);

scanf(“%c”, &lower);

upper=lowertoupper (lower);

printf(“\n The uppercase equivalent is %c\n\n”, upper);

}

# Write a program to accept a sentence and print it.

#include <stdio.h>

void main ()

{

char c[80];

printf(“\n Enter a sentences:”);

gets (c);

printf(“\n The entered sentences is :\n”);

puts (c);

}

# Write a program to print the first word of a string though you input a single word or sentence.

#include <stdio.h>

#include <string.h>

void main ()

{

char a[10];

printf(“\n Enter a string”);

scanf(“%s”, a);

printf(“\n You entered a string: %s”, a);

}

After running this program, you will get only the first word of the sentence. The compiler terminates the other characters encountered immediately after the first white space.

The format specifier %s accepts a word. The texts separated after the word with space are truncated. Hence, the program prints a single word.

# Write a program to enter name of 5 persons and display on the screen.

#include <stdio.h>

#include <ctype.h>

#include <string.h>

main ()

{

char name [5] [30];

int i;

for (I =0; i< =4; ++i)

printf(“\n Enter name :”)

gets (name [i] );

for (i=0; i<=4; i++)

printf(“\n The name is %s” , name[i]);

}

}

In the above program, the line char name [5] [30] handles names of five persons each with a length of 30 characters including spaces and other printable characters. The line gets (name [i] accepts the names of 5 persons where the value of i goes on changing from 0 to 4. The gets() function handles the string of any length. In other words, it accepts or gets the string to get string =gets). We can use the puts () function to display the string which is used in an example.

# Write a program to sort the given strings in ascending order.

#include <stdio.h>

#include <conio.h>

#include <alloc.h>

#include <string.h>

#include <ctype.h>

#define MAXLEN 80

viod sort_strings (char * strarr[], int n)

{

int i, j, next, pass_limit, sorted_flag=0;

for(i=0; i<n && !sorted_flag; i++)

{

pass_limit = n-1;

sorted_flag=1;

for (j=0; j<pass_limit; j++)

{

/* if one string is greater than the next, swap the strings */

next = j+1;

if (strcmp (strarr[j], strarr[next])>1)

{

char *temp = strarr[j];

strarr[j] =strarr[next];

strarr[next] =temp;

sorted_flag=0;

}

}

}

}

void main ()

{

int i, n;

char **str_array;

printf(“\n Number of strings:”);

scanf(“%d”, &n);

str_array=(char**) malloc (n *sizeof (char*));

printf(“Enter a string:\n”);

for (i=0; i<n; i++)

{

str_array[i] =(char*) malloc (MAXLEN);

scanf(“%s”,str_array[i]);

}

sort_strings(str_array,n);

printf(“\n The sorted array of string is:\n”);

for (i=0; i<n; i++)

{

printf(“%s\n”, str_array[i]);

}

}

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 231-241.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

  1. The array can be passed as arguments to functions. An array can be passed as an argument from one function to another.
  2. Two parameters are required an array parameter a to catch the array passed and a parameter n to catch the index of the last item in the array to be summed.
  3. The parameter declaration for the array includes square brackets to signal the functions sum that a is an array name and not the name of an ordinary parameter.
  4. The base address of array elements can be passed to the called function using address operator &. After the base address is passed to the called function, pointer variables can access other elements of an array.
  5. A string is the sequence of characters placed one after the other terminated with a null character’\0’.
  6. It is true that some characters within a string be processed individually (e.g. Piglatin). However, most of the strings are processed as complete entities. The strings are processed using string manipulating functions.
  7. C compilers include library functions that allow strings to be compared, copied or concatenated. Other library functions permit operations on individual characters within string. 
.

Very Short Questions

0%

DISCUSSIONS ABOUT THIS NOTE

No discussion on this note yet. Be first to comment on this note