Question

Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...

  1. Consider the following function:
    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.
  2. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces)
  3. Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program).
  4. Write a single function that finds both the largest and the smallest elements of an integer array.

Can these be done in C?

Homework Answers

Answer #1

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 !

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
How would I write the following program in C programming language? Function header: int sumsort(int *a,...
How would I write the following program in C programming language? Function header: int sumsort(int *a, int *b, int *c) This function should arrange the 3 values in the memory locations pointed to by a, b, and c in ascending order and also return the sum of the contents of the memory locations a, b, and c.
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c =...
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c = &array[5];   c[-1] = 2;   array[1] = b[1]-1;   *(c+1) = (b[-1]=5) + 2;   sum += *(a+2) + *(b+2) + *(c+2);   printf("%d %d %d %d\n", sum, array[b-a], array[c-a], *a-*b); array[8]={ ?? }; what is array[8]?
a) sum = 0 for letter in '12345' : sum = sum + int(letter) print('sum =...
a) sum = 0 for letter in '12345' : sum = sum + int(letter) print('sum = ', sum) Write a program which reads a string containing any characters from the user and prints the sum of the digits in the string. For example, if the string from the user is "I want 3 oranges and 24 bananas', then the output would be 9, since 3 + 2 + 4 = 9. Note that a character C is a digit if...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() { char str[100]; /*Buffer to hold reversed string */ reverse("cat", str); /*Reverse the string "cat" */ printf("reverse(\"cat\")=%s\n", str); /*Display result */ reverse("noon", str); /*Reverse the string "noon" */ printf("reverse(\"noon\")=%s\n", str); /*Display result */ } reverse(char *before,...
Given the following code snippet, what is this program calculating? for (int i = 0; i...
Given the following code snippet, what is this program calculating? for (int i = 0; i < n; i++) { sum += arr[i]; } sum /= n;
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT