Question

Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at...

Write a program that finds (ANSWER IN C LANGUAGE!):


1. The sum of all elements at even subscripts
2. The sum of all elements at odd subscripts
3. The sum of all elements


You are allowed to perform this functionality within main.

Main program:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>

int main()
{
/* Declare array variables */
const int NUM_VALS = 4;
int userValues[NUM_VALS];
int i; // counter
int even = 0;
int odd = 0;
int sum = 0;

/* Initialize the array with values */
userValues[0] = 2;
userValues[1] = 5;
userValues[2] = -1;
userValues[3] = 7;

return 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
======================
Sample Output:
======================
Sum of all elements at even subscripts = 1
Sum of all elements at odd subscipts = 12
Sum of all elements = 13

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

The code given below asks user to enter the number of values and then asks user to enter the values in the array.

In the code given below following steps are followed :

1. Calculate the sum of all the elements and store the answer in the sum variable.( for loop is used for this )

2. Calculate the sum of all the elements at even subscripts. To do this, modulus operator ( % ) is used to calculate the remainder after dividing subscript (index of array ) by 2 and if remainder is 0 then it is an even subscript. And so the sum of all the elements at even subscripts is calculated.

3. Sum of all the elements at odd subscripts is calculated by subtracting the sum of elements at even subscripts from the total sum of the elements .

odd = sum - even // here the sum variable stores the total sum of all the elements in the array.

/* Code starts from here */

#include <stdio.h>

int main()
{
    int NUM_VALS = 0; //variable to store the number of elements in the array.
    printf("Input:\n");
    printf("Enter the number of elements: ");
    scanf("%d",&NUM_VALS);
    int userValues[NUM_VALS]; //declaring array
    int i; // counter
  
    /*
    defining the variables to store the sum of all elements ,
    elements at even subscripts and elements at odd subscripts
    */

    int even = 0;
    int odd = 0;
    int sum = 0;


printf("Enter the values in the array: ");
for(i=0;i<NUM_VALS;i++)
{
    scanf("%d",&userValues[i]);
}

//calculating the sum of all elements
for(i=0;i<NUM_VALS;i++)
{
    sum += userValues[i];
}

//calculating the sum of all elements at even subscripts
for(i=0;i<NUM_VALS;i++)
{
    if((i%2)==0)
    {
        even = even + userValues[i];
    }
}
//calculating the sum of all elements at odd subscripts
odd = sum - even;

printf("\nOutput: \n");
printf("Sum of all elements at even subscripts = %d\n",even);
printf("Sum of all elements at odd subscripts = %d\n",odd);
printf("Sum of all elements = %d\n",sum);

    return 0;
}

/* Code ends here */

The code given below completes the code given in the question. It does not asks the user for the number of elements in the array. Number of elements in the array is initialized at the beginning and the elements in the array are also initialized .

It simply calculates the sum of all the elements , sum of elements at even subscripts and sum of elements at odd subscripts and prints the answer on the screen.

/* Code starts from here */

#include <stdio.h>

int main()
{

/* Declare array variables */
const int NUM_VALS = 4;
int userValues[NUM_VALS];
int i; // counter
int even = 0;
int odd = 0;
int sum = 0;

/* Initialize the array with values */
userValues[0] = 2;
userValues[1] = 5;
userValues[2] = -1;
userValues[3] = 7;
//calculating the sum of all elements
for(i=0;i<NUM_VALS;i++)
{
    sum += userValues[i];
}

//calculating the sum of all elements at even subscripts
for(i=0;i<NUM_VALS;i++)
{
    if((i%2)==0)
    {
        even = even + userValues[i];
    }
}
//calculating the sum of all elements at odd subscripts
odd = sum - even;

printf("\nOutput: \n");
printf("Sum of elements at even subscripts = %d\n",even);
printf("Sum of elements at odd subscripts = %d\n",odd);
printf("Sum of all elements = %d\n",sum);

    return 0;
}


/* Code ends here */

Screenshots:

Add a comment
Know the answer?
Add Answer to:
Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at...
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
  • Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements....

    Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 2, 1, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: * matchValue: 2, userValues: {2, 2, 1, 2} (as in the example program above) * matchValue: 0, userValues: {0, 0, 0, 0} * matchValue: 50, userValues: {10, 20, 30, 40} (Notes) #include <iostream> #include <vector> using namespace...

  • C++.Write the body of a function that returns the sum of all elements in a two-dimensional...

    C++.Write the body of a function that returns the sum of all elements in a two-dimensional array passed to it as an argument. Declare and initialize all needed variables. int arraySum(const int numbers[][COLS], int rows) { }

  • Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....

    Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps. This is a basic course and the furthest we have gone is BASIC arrays. Write the code for the following problems using arrays. Write what would go in the “int main()” part of the program. For these problems, the entire program and program output is not needed. Just include what goes in the int main() part of the program. 1. Declare an integer array...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • 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...

  • 1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS...

    1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 1, 2, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: matchValue: 2, userValues: {2, 1, 2, 2} (as in the example program above) matchValue: 0, userValues: {0, 0, 0, 0} matchValue: 10, userValues: {20, 50, 70, 100} What i am given: import java.util.Scanner; public class FindMatchValue...

  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

  • 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...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • 1.Write an assembly language program that corresponds to the following C++ program: #include <iostream> using namespace...

    1.Write an assembly language program that corresponds to the following C++ program: #include <iostream> using namespace std; const int amount = 20000; int num; int sum; int main () { cin >> num; sum = numb + amount; cout << "sum = " ,<< sum << endl; return 0; } 2. Test the program in the previous question twice. The first time, enter a value for num to make the sum within the allowed range for the Pep/8 computer. The...

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