Hello all

I have been learning C for about 3 - 4 weeks now and I'm in need of some help.

I am trying to write a function that will give you the sum of selected integers in an array with a value of 20. I can make it give me a total for all of the intergers but not when i select certain integer positions, ie: say array positions 0 to 4 or array positions 15 to 19.

This is what I have so far
Function Protocol
int sum(int[], int, int);

Function Definition

int sum(int num_array1[], int start, int end)
	{
		int total=0;


		for(int a=start; a<=end; a++)
		{
			total = total + num_array1[a];
			
			return(total);

Thanks for any suggestions

Dani AI

Generated

Good catch by — returning from inside the for loop causes exactly the symptom saw (only the first element is returned). ’s comment about missing braces and ’s prompt to show main were also useful: small posting/typo errors (undeclared variables, misplaced returns) commonly mask the real bug, so always fix compilation warnings and re-check the variables you use in loops.

A few practical improvements beyond moving the return out of the loop:

  • Decide whether your end index is inclusive or exclusive and document that choice (current code used inclusive <=).
  • Pass the array length to the function and validate start/end before accessing the array to avoid out-of-bounds reads.
  • Use an appropriately wide accumulator (e.g., long long) if the array can contain many or large values to reduce overflow risk.

Example of a safer function and how to call it:

#include <stddef.h>
#include <stdint.h>

long long sum_range(const int *arr, size_t n, size_t start, size_t end) {
    if (!arr || start > end || end >= n) return 0; /* or use explicit error reporting */
    long long total = 0;
    for (size_t i = start; i <= end; ++i) total += arr[i];
    return total;
}

Compute n at the call site for stack arrays with sizeof arr / sizeof arr[0]. If you need to distinguish an error from a legitimate zero sum, return an error code (or use an out-parameter) instead of returning 0 on invalid input.

Quick checklist when debugging similar problems: move return statements out of loops, enable compiler warnings (-Wall -Wextra), validate indices before use, and consider runtime sanitizers (address/undefined) when behavior looks wrong.

Recommended Answers

All 6 Replies

how did that even compile? It is missing two closeing } character. Assuming that is just a posting error the code looks ok to me.

The missing } were just posting error.

When I call the function in main the only answer I get is what the first array position is.

total=sum(num_array1,0,4);
printf("answer = %d \n", total);

Post your main array definition. Your code should work as expected.

ok, this is what i have in main

int main()
{
	int num_array1[] = {7,9,16,2,53,74,31,14,13,8,95,34,17,41,21,29,33,1,11,105} ;
	int answer = 0;
	int i=0;
	int j=0;
	int total;


	for(i=0; i<=end; i++)
	{

		printf("position 0= %d \n", num_array1[0]);
		printf("position 4 = %d \n", num_array1[4]);
		total=sum(num_array1, 0, 4);
		printf("answer = %d \n", total);

		printf("position 5= %d \n", num_array1[5]);
		printf("position 9 = %d \n", num_array1[9]);
		total=sum(num_array1, 5, 9);
		printf("answer = %d \n", total);
		
	}

	for (j=0; j<20; j++ )
	{
		answer += num_array1[j];
	}
	printf("answer = %d \n", answer);


	return(0);

}

the answer i get for the first sum is 7 the second sum is 74 and the grand total is 614

Thanks for an suggestions

Could it be that your return statement in the sum-function is in the for-loop?
It is indented that way.

commented: good observation :) +36

I changed the return to outside the for loop in the Function Definition and that did the trick.

Thanks...I didn't notice that before

int sum(int num_array1[], int start, int end) // Function Definition
	{
		int total=0;

		for(int a=start; a<=end; a++)
		{
			total = total + num_array1[a];

		}

		return(total);
		
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.