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
#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.
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.
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...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Hello, I'm having a hard time with the below array problem. I declare and initialized the...
Hello, I'm having a hard time with the below array problem. I declare and initialized the array, but when I create the command line argument I get syntax error. I can't get pass there. This is what I have so far: int [][] movieRatings; movieRatings = new int [args.length][args.length]; for (int i = 0; i < args.length; i++) movieRatings[i] = Integer.parseInt(args[i]); //getting a error here :( movieRatings[2] = new int [4]; Rotten Tomatoes (20 points). Write a program RURottenTomatoes.java that...
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum...
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum of the digits of n. Use the following main function to test your code: int main() { int n, sn; cout << "Enter q to quit or an integer: "; while ( cin >> n ) { sn = sumofdigits(n); cout << "sumofdigits( " << n << " ) = " << sn; cout << "\nEnter q to quit or an integer: "; }...
Question #15. Write a function file that determines the following sum using a for loop: y=i=1n(3-2i)2...
Question #15. Write a function file that determines the following sum using a for loop: y=i=1n(3-2i)2 Use n as input and test the function for n=100. Question #15. Write a function file that determines the following sum using a for loop: y=i=1n(3-2i)2 Use n as input and test the function for n=100.
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...