Question

C programming3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use of pointers), to pass the array to a function,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
//defination of summation()
double summation(int *f)
{
    int i;
    double sum=0;
    for(i=0;i<10;i++)
    { //find the sum by multiplying the element by 3
        if(i%2==0)
        sum= sum + *(f+i) * 3;
        else
        //find the sum by multiplying the element by 2
        sum = sum + *(f+i) * 2;
   }
   //multiply by 1/2
   sum = 0.5 * sum;
   //return the sum
   return sum;
}
int main()
{
    int *f,i;
    double sum;
    //allocate 10 memory cells for the array dynamically
    f= (int *)malloc(10 * sizeof(int));
    //loop to input the array elements
    for(i=0;i<10;i++)
    {
        printf("\n Enter a number");
        scanf("%d",f+i);
   }
   //display the array elements
   printf("\n The array elements are \n");
   for(i=0;i<10;i++)
   printf(" %d ",*(f+i));
   //call to the summation() method by call by reference
   sum=summation(f);
   //display the sum according to the formula given
   printf("\n Sum of the array elements according to the formula is %5.2lf",sum);
}

OUTPUT

CAUserstROZYnKRISHNADesktopisum.exe Enter a number2 Enter a number3 Enter a number 5 Enter a number6 Enter a number8 Enter a

note : in question the formula is not clearly visible . I have multiply 3 and 2 . If that symbol is not '*' so just change the statement sum= sum + *(a+i) * 3; to sum= sum + *(a+i) + 3; and sum= sum + *(a+i) * 2; to sum= sum + *(a+i) + 3;

Add a comment
Know the answer?
Add Answer to:
C programming 3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array...

    Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array to a function, named Summation. In main: Define array, pass array, print out the array and the result returned from Summation on screen. In function Summation: 1)take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4)if the value of f(i) is negative, add -3*f(i) to...

  • Define two arrays x and f. cach of size 10. using call-hy-reformer (that is, use pointers),...

    Define two arrays x and f. cach of size 10. using call-hy-reformer (that is, use pointers), to pass the array to a function. named sum. In main: define array, pass arrays, print out the array and the results on screen In function sum, take arrays from main and sum the arrays using the formula below: Sum = sigma_1^10 e^x1 + x_1^3 - 3x_162 + 5x_i

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • C programming Write a function that goes through all elements of an array of size n,...

    C programming Write a function that goes through all elements of an array of size n, and output the minimum and maximum values using pointers. Since C functions can only return one value, you must use pointers to output the minimum and maximum values from the function. You are not expected to print the values on the screen. Note: you will be assessed on the functionality and the coding style.

  • Write a C code to perform the following tasks: In main: Define an array of SIZE...

    Write a C code to perform the following tasks: In main: Define an array of SIZE = 7 and initialize them with 3, 5, 7, 9, 11, 13, 15 Call function sum with the array and size as parameters Print each element of the array Print the sum In function sum. Use for-loop to calculate sum

  • B. Which statement would be used to define a to element integer array c? a. Array...

    B. Which statement would be used to define a to element integer array c? a. Array c = int[10]; b. C = int[10]; c. int Array c[10]; d. int c[10]; 17. Declare an Integer pointer variable named int_ptr. 18. Use the pointer created in question 17 and assign it the memory location of a variable named int_var. 19. Suppose you have an executable program named count that counts the characters in its input. Devise a command-line command using the count...

  • Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as...

    Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its size to pointers; depending on...

  • Use C: 3. In function main, declare a two-dimensional integer array with 5 rows and 4...

    Use C: 3. In function main, declare a two-dimensional integer array with 5 rows and 4 columns. Call the following functions from function main. Call a function createArray to seed the random number generator and to fill the two-dimensional array with random integers between -20 and +20. Parameters for this function should be the array and the number of rows and columns). Call a function signs to count the number of positive values, number of negative values and number of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT