w3resource

C Exercises: Compute the sum of values in a given array of integers except the number 17

C-programming basic algorithm: Exercise-52 with Solution

Write a C program to compute the sum of values in a given array of integers except the number 17. Return 0 if the given array has no integers.

C Code:

#include <stdio.h>
#include <stdlib.h>
int main(void){
    int array1[] = {1, 2, 5, 7, 9, 10, 12, 17};
    int arr_size = sizeof(array1)/sizeof(array1[0]);
    printf("Sum of values in the array of integers except the number 17: ");
    printf("%d",test(array1, arr_size));
  }    
  int test(int nums[], int arr_size)
         {
            int sum = 0;

            for (int i = 0; i < arr_size; i++)
            {
                if (nums[i] != 17) sum += nums[i];
                else i++;
            }
            return sum;
         }

Sample Output:

Sum of values in the array of integers except the number 17: 46

Pictorial Presentation:

C Programming Algorithm: Compute the sum of values in a given array of integers except the number 17

Flowchart:

C Programming Algorithm Flowchart: Compute the sum of the two given integer values

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program to count even number of elements in a given array of integers.
Next: Write a C program to compute the sum of the numbers in a given array except those numbers starting with 5 followed by atleast one 6. Return 0 if the given array has no integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Where in memory are my variables stored in C?

You got some of these right, but whoever wrote the questions tricked you on at least one question:

  • global variables -------> data (correct)
  • static variables -------> data (correct)
  • constant data types -----> code and/or data. Consider string literals for a situation when a constant itself would be stored in the data segment, and references to it would be embedded in the code
  • local variables(declared and defined in functions) --------> stack (correct)
  • variables declared and defined in main function -----> heap also stack (the teacher was trying to trick you)
  • pointers(ex: char *arr, int *arr) -------> heap data or stack, depending on the context. C lets you declare a global or a static pointer, in which case the pointer itself would end up in the data segment.
  • dynamically allocated space(using malloc, calloc, realloc) --------> stack heap

It is worth mentioning that "stack" is officially called "automatic storage class".

Ref : https://bit.ly/33PQ3g8

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook