Question

Write a C code to perform the following tasks: In
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Sum.c

#include <stdio.h>
void sum(int a[], int size){
int i;
int sum = 0;
printf("Array elements are: \n");
for(i=0; i<size; i++){
printf("%d ", a[i]);
sum = sum + a[i];
}
printf("\nSum is %d\n", sum);
}
int main()
{
int a[7] = {3,5,7,9,11,13,15};
  
sum(a,7);
return 0;
}

Output:

sh-4.3$ main Array elements are: 3 5 7 9 11 13 15 Sum is 63 sh-4.3$

Add a comment
Know the answer?
Add Answer to:
Write a C code to perform the following tasks: In main: Define an array of SIZE...
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...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • Write out C++ code for the following: Declare a templated function that has a return type...

    Write out C++ code for the following: Declare a templated function that has a return type of T". It has two parameters: An array of T items named arr, and an integer named size. This function is assumed to work with numerical data types. Within the function, create a new T variable named sum and initialize it to 0. Use a for-loop to go from 0 (inclusive) to size (exclusive), adding each element from arr to the sum. Finally, return...

  • C++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop...

    C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with...

  • Step 1. Start Netbeans and create a project called ArrayLab Step 2. Write the main method...

    Step 1. Start Netbeans and create a project called ArrayLab Step 2. Write the main method  Declare and initialize an array called myNums int myNums = {3, 8, 12, 4, 2, 9, 6};  Declare an int called largestNum and set it to 0.  Write a for loop that prints the array for (int index = 0; index < myNums.length; index++) { System.out.println(myNums[index] + “ “); }  Write a for loop that prints the array backwards ...

  • C programming 3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use...

    C programming 3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use of pointers), to pass the array to a function, named summation In main: define array, pass array, print out the array and the results on screen In function summation: take array from main and sum the array using the formula below: 10 fO 2 246 NOTE: you must use call-by-reference (the pointer) to call functions, as required; otherwise, no credits will be given.

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