Question Write a program which performs the following tasks:
− initialize an integer array of 10 elements in main( )
− pass the entire array to a function modify( )
− in modify( )multiply each element of array by 3
− return the control to main( )and print the new array
elements in main( )
Solution-
Fundamental Question, let's check up the solution. Notice that i used the call by reference and passing array to a function techniques to make this program.we should use these ways of programming more often, because A Good Programmer Always Follows the Best Pathway Possible.
#include <stdio.h>
#include <stdlib.h>
#define m 10
int main()
{
int arr[m];
int i;
printf("Enter the elements of the array\n");
for(i=0;i<m;i++)
scanf("%d",&arr[i]);
modify(&arr);
printf("New array is given by\n");
for(i=0;i<=m-1;i++)
printf("%d ",arr[i]);
getch();
}
modify(int *arr[m])
{
int i,k;
for(i=0;i<=m-1;i++)
{
k=arr[i];
k=k*3;
arr[i]=k;
}
}
− initialize an integer array of 10 elements in main( )
− pass the entire array to a function modify( )
− in modify( )multiply each element of array by 3
− return the control to main( )and print the new array
elements in main( )
Solution-
Fundamental Question, let's check up the solution. Notice that i used the call by reference and passing array to a function techniques to make this program.we should use these ways of programming more often, because A Good Programmer Always Follows the Best Pathway Possible.
#include <stdio.h>
#include <stdlib.h>
#define m 10
int main()
{
int arr[m];
int i;
printf("Enter the elements of the array\n");
for(i=0;i<m;i++)
scanf("%d",&arr[i]);
modify(&arr);
printf("New array is given by\n");
for(i=0;i<=m-1;i++)
printf("%d ",arr[i]);
getch();
}
modify(int *arr[m])
{
int i,k;
for(i=0;i<=m-1;i++)
{
k=arr[i];
k=k*3;
arr[i]=k;
}
}
No comments:
Post a Comment