I need to do two things to an array in C:
- Summing its entries.
- Finding the position of all non-zero entries.
I am doing both by looping through each element of the array. E.g.
int sum_array(int a[], int num_elements)
{
int i, sum=0;
for (i=0; i<num_elements; i++)
{
sum = sum + a[i];
}
return(sum);
}
For task 2, I will be checking if the entry is 0. If not, add its position to another array to mark it as non-zero.
Question
Are there more efficient ways of doing these?
I am also using the GNU Scientific Library (GSL), if there are more efficient solutions in there. I just started C recently.
Based on the comments from Gregor, EvilTeach and some of my own googling, my approach is acceptable.