C Programming: Calculating the Sum of Positive Integers in an Array

How to Calculate the Sum of Positive Integers in an Array Using C Programming

Introduction

C is a powerful and versatile programming language used for a wide range of applications, from systems programming to algorithmic development. One such application is calculating the sum of positive integers within an array. This task is not only educational but also serves as a fundamental exercise in programming. In this article, we will discuss how to perform this operation step by step using C programming.

Step-by-Step Guide

To calculate the sum of positive integers in an array using C, follow these steps:

Step 1: Initialize Variables

The first step is to initialize a variable to hold the sum. This variable will accumulate the sum of positive integers as you iterate through the array.

// Initialize the sum variable to zeroint sum  0;

Step 2: Loop Through the Array

Next, use a for loop to iterate through each element of the array. The loop will continue until it has processed all elements.

// Loop through each element of the arrayfor (int i  0; i  size; i  ) {

Step 3: Check for Positive Integers

Within the loop, use an if statement to check if the current element is positive. If the element is positive, add it to the sum.

    // Check if the current element is positive    if (arr[i]  0) {        // Add the element to the sum        sum   arr[i];    }

Step 4: Print the Result

After the loop has processed all elements, print the sum of positive integers.

// Print the resultprintf("The sum of positive integers is: %d", sum);

Example Code

Here is a simple example of how to calculate the sum of positive integers in an array:

#include stdio.hint main() {    int arr[]  {1, -2, 3, 4, -5, 6}; // Example array    int size  sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements    int sum  0; // Initialize sum    // Loop through the array    for (int i  0; i  size; i  ) {        // Check if the current element is positive        if (arr[i]  0) {            sum   arr[i]; // Add to sum if positive        }    }    // Print the result    printf("The sum of positive integers is: %d", sum);    return 0;}

Breakdown of the Code

Let's break down the code to understand each step in more detail:

Array Declaration

The array arr is initialized with both positive and negative integers.

int arr[]  {1, -2, 3, 4, -5, 6};

Size Calculation

The size of the array is computed using sizeof(arr) / sizeof(arr[0]).

int size  sizeof(arr) / sizeof(arr[0]);

Sum Initialization

The variable sum is initialized to zero.

int sum  0;

Loop

A for loop iterates through each element of the array.

for (int i  0; i  size; i  ) {

Condition Check

The if statement checks if the current element is greater than zero.

if (arr[i]  0) {

Sum Update

If the condition is true, the element is added to sum.

sum   arr[i];

Output

Finally, the sum of positive integers is printed.

printf("The sum of positive integers is: %d", sum);

Modifying the Array

You can modify the array values to test with different sets of integers. Here is an example with a different set of integers:

#include stdio.hint main() {    int arr[10]  {2, -3, 4, 6, -1, 8, -9, 3, -2, 1}; // Example array    int size  sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements    int sum  0; // Initialize sum    // Loop through the array    for (int i  0; i  size; i  ) {        // Check if the current element is positive        if (arr[i]  0) {            sum   arr[i]; // Add to sum if positive        }    }    // Print the result    printf("The sum of positive integers is: %d", sum);    return 0;}

This example demonstrates how you can easily modify the array to test different values.

Conclusion

Calculating the sum of positive integers in an array using C programming is a straightforward task that helps in understanding the basics of array manipulation and conditional statements. This skill is crucial for more complex programming tasks and is a great starting point for beginners in C programming.