double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; }Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead.
Can these be done in C?
As per your requirement,here is the C code:
1.ArrayAverage
#Rawcode
//including header files
#include <stdio.h>
#include <conio.h>
//declaration of function
double arrayavg(int a[],int n);
//main function
void main()
{
//declaring variables
int n,i;
//declaring array of size n(which we take from
user
int a[n];
//asking user to enter length
printf("Enter array Length\t");
//scaning length of array to variable n
scanf("%d",&n);
//for loop to read array elements
for(i = 0;i<n;i++){
printf("Enter the element %d\t",i);
scanf("%d",&a[i]);
}
//calling function and storing the output in variable result
double result = arrayavg(a,n);
printf("%f",result);
}
//function which calculates average of array elements
double arrayavg(int a[],int n){
int *p,i,sum=0;
p = a;
for(i=0; i!=n; ++i){
sum=sum+*p;
p++;
}
return (double)sum/n;
}
Here is the screenshot of source code:
Here is the screenshot of output of above code:
2.Palindrome
#Rawcode
//including header files
#include <stdio.h>
#include <string.h>
//defining MAX_LIMIT value 200
#define MAX_LIMIT 200
main(){
char n[MAX_LIMIT];
//asking user for input
printf("Enter input ");
//reading input including spaces
gets(n);
int left = 0;
int right = strlen(n);
//loop which checks given input is palindrome or
not
while(left<right){
while (!isalnum(n[left]) &&
left<right){
left++;
}
while (!isalnum(n[right]) &&
left<right){
right--;
}
if (tolower(n[left++]) != tolower(n[right--])) {
printf("Given input is not a Palindrome\n");
}
}
printf("Given input is a palindrome\n");
}
Hers is the screenshot of source code:
Here is the screenshot of output of above code:
3. takes the name of a file as a command line argument, and prints the contents of the file :
#Rawcode
//including headers
#include <stdio.h>
#include <stdlib.h>
int main()
{
//file pointer
FILE *ptr;
char text,fileName[100];
printf("Enter name of a file you wish to see\n");
//getting file name
gets(fileName);
//opening file in reading mode
ptr = fopen(fileName,"r");
//if file can't be opened printing error
if(ptr == NULL)
{
printf("Error!");
}
text = fgetc(ptr);
//while loop which iterates untill the END OF FILE
while(text!=EOF){
printf("%c",text);
text= fgetc(ptr);
}
//closing file
fclose(ptr);
return 0;
}
Here is the screenshot of source code:
Here is the screenshot of output of above code:
Note:The opening file and the program file should be in same folder.
4.Finding largest and smallest element in an integer array:
#Rawcode
//including header file
#include <stdio.h>
//declaration of function
void findSmallestAndLargest(int [],int);
main(){
//declaring variables
int length,i;
int arr[length];
//asking user to enter array length
printf("Enter array Length ");
//scanning length of array into variable length
scanf("%d",&length);
//for loop which reads array elements from user
input
for(i = 0;i<length;i++){
printf("Enter the element %d\t",i);
scanf("%d",&arr[i]);
}
//calling funcion to find largest and smallest elements in
array
findSmallestAndLargest(arr,length);
}
//function to find largest and smallest elements in array
void findSmallestAndLargest(int arr[],int length){
//declaring variables
int i;
int smallest,largest;
largest = smallest = arr[0];
//for loop which checks for largest and smallest
elements in array
for(i=1;i<length;++i){
if(arr[i]>largest){
largest =
arr[i];
}
if(arr[i]<smallest){
smallest =
arr[i];
}
}
printf("The largest element is %d \n",largest);
printf("The smallest element is %d ",smallest);
}
Here is the screenshots of source code:
Here is the screenshot of ouput of above code:
Hope these will help you .If ypu still have any queries feel free to ask in comment section.
Make use of screenshots of source code for better understanding of indentation.
Do upvote.Thank you !
Get Answers For Free
Most questions answered within 1 hours.