Jump to content

C accumulator help

duckwithanokhat

So I have this code 

int index, mean = 0;
    int response[99] = {6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
    7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
    7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
    7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    4, 5, 6, 1, 6, 5, 7, 8, 7};

    for(index = 0; index < 100; index++)
        mean += response[index];

    printf("%d", mean);

Which basically adds every number in the array and prints the result. I'm somehow getting 1362 as my output. However, the highest number is 9, so even if every number was 9 you couldn't get 1362.

 

What am I doing wrong?

 

EDIT: Never mind I found it out, in the for loop it should be index < 99

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, duckwithanokhat said:

EDIT: Never mind I found it out, in the for loop it should be index < 99

Actually, you should make it a habit of removing magic numbers from your code, a possible solution would be:

int index, mean = 0;

//Remove the array size from the brackets, now the array will
//automatically be the size of the number of elements you 
//initialise it with.
int response[] = {	6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    			7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    			6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
    			7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    			6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
    			7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    			5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    			7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
   			7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    			4, 5, 6, 1, 6, 5, 7, 8, 7	};

//Divide the size of the array by the size of a int to get the number of
//elements, this will now work for any array size without needing to change.
//Since these are all compile time constants there will be no performance penalty.
for(index = 0; index < (sizeof(response) / sizeof(int)); index++)
	mean += response[index];

printf("%d", mean);

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Unimportant said:

Actually, you should make it a habit of removing magic numbers from your code, a possible solution would be:


int index, mean = 0;

//Remove the array size from the brackets, now the array will
//automatically be the size of the number of elements you 
//initialise it with.
int response[] = {	6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    			7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    			6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
    			7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    			6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
    			7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    			5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    			7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
   			7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    			4, 5, 6, 1, 6, 5, 7, 8, 7	};

//Divide the size of the array by the size of a int to get the number of
//elements, this will now work for any array size without needing to change.
//Since these are all compile time constants there will be no performance penalty.
for(index = 0; index < (sizeof(response) / sizeof(int)); index++)
	mean += response[index];

printf("%d", mean);

 

I believe the Visual C/C++ compiler (and possibly others) have a _countof(...) macro.

If you're working with C++ you should use an std array and a range based for loop, or even better: std::accumulate

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

Let me elaborate some more. C is made to work close to the hardware, which causes it to do some funky things that you might not expect.

First of all, arrays are 0 based indexed. This is because of the pointer math the CPU has to execute in the background. To get the first element of an array you use [0], for the second element you use [1], etc.

Now in your program, there is another funky thing going on, which is called 'undefined behavior'. It is basically invalid code, that will compile fine, but give unexpected results. The C standard basically says, if something is defined as undefined behavior, the computer is allowed to do anything. It could even open up paint, draw a llama, and it would still be following the C standard! The undefined behavior that is going on in your program is going out of bounds in your array. The array has 99 elements in it, you for loop ranges from 0 to 99. 0 meaning the first element, 99 meaning the 100th element. This means that you are reading outside of you array! The people above me have pointed out good ways to avoid this problem, but I wanted to go a bit more in-depth. Since going outside of your array is undefined behavior, the program was giving you a completely valid value back! You would not come across these type of issues in a language like C# of javascript because they define what happen when your program goes out of bounds. The quickest fix for you program is to only loop from 0 to 99, that way you will not go out of bounds.

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, Lijmer said:

Let me elaborate some more. C is made to work close to the hardware, which causes it to do some funky things that you might not expect.

First of all, arrays are 0 based indexed. This is because of the pointer math the CPU has to execute in the background. To get the first element of an array you use [0], for the second element you use [1], etc.

Now in your program, there is another funky thing going on, which is called 'undefined behavior'. It is basically invalid code, that will compile fine, but give unexpected results. The C standard basically says, if something is defined as undefined behavior, the computer is allowed to do anything. It could even open up paint, draw a llama, and it would still be following the C standard! The undefined behavior that is going on in your program is going out of bounds in your array. The array has 99 elements in it, you for loop ranges from 0 to 99. 0 meaning the first element, 99 meaning the 100th element. This means that you are reading outside of you array! The people above me have pointed out good ways to avoid this problem, but I wanted to go a bit more in-depth. Since going outside of your array is undefined behavior, the program was giving you a completely valid value back! You would not come across these type of issues in a language like C# of javascript because they define what happen when your program goes out of bounds. The quickest fix for you program is to only loop from 0 to 99, that way you will not go out of bounds.

Thanks for the explanation.

Link to comment
Share on other sites

Link to post
Share on other sites

C can be pretty horrible to program in if you don't know about all the pitfalls. However, once you get more experienced you learn where these pitfalls are and how you can avoid them. Most of these pitfalls are there for a very good reason. If you want to know more about these C (and C++) pitfalls, I can recommend you Deep C: http://www.slideshare.net/olvemaudal/deep-c.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×