+ 2
Array in c programming
The given code declares an array, that corresponds to game results of a basketball team. 1 represents a win, while 0 represents a loss. The team has played a total of 28 games. Task: Create a program that calculates and outputs the number of win for the given team.
8 odpowiedzi
+ 7
Prakash Kumar Jaiswal another way to solve it would be to replace the if statement with
count += result[i];
Even though it would perform the addition in every iteration, it still is likely to be faster than using an if condition that adds only when necessary. The explanation is due to technicalities with machine instruction pipelining in modern CPUs. Branching with an if statement disrupts the flow of instruction fetching as the fetcher has to predict which way the conditional will go. If it is wrong, then it has to delay processing while it flushes the pipeline and loads the correct branch of instructions.
In general, this means it is generally faster if you can avoid if statements even if it entails making a number of extra calculations. But really it is fewer calculations because it is no longer doing comparision AND calculation, or comparison AND branching. Now it is purely doing calculation that furthermore can be better queued up in parallel processing.
+ 4
Prakash Kumar Jaiswal ,
your code is working properly according the task description. i can not see any question either.
what is the purpose of this post? `q&a` forum is for coding related questions or discussions.
if you are just going to present your code here, this is not the rigth place.
+ 3
Your solution is fine as it is.
In many cases, there are more than one way to do things. Some techniques require more CPU time or memory than other techniques for a given problem. Your solution is efficient. You could have written it in other ways that are less efficient, but you did not. This is great.
+ 2
Brian ,
Thanks for your valuable advice 🙂
+ 1
#include <stdio.h>
int main() {
int results[] = {0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1};
int count=0;
for(int i=0; i<28; i++){
if(results[i]==1){
count++;
}
}
printf("%d",count);
return 0;
}
+ 1
Jerry Hobby ,
Thank you so much for your support and appreciation 🙏
0
Sorry I just recently reached out to the solution of this problem. But if you have any other ways to solve this problem in c programming, please solve it. I want to see various aspects to solve this problem!
0
Hi