I just can't make simple algorithms for simple problems

Hi everyone, I’m here to ask to anyone some tips or advice for my situation.
Yes, it’s exactly how I mentioned in the title, I simply can’t make simple algorithms. Even for the most basic problems.

Can you spare some time to see an example?
I have this problem: read a number X until this number equals to zero. For each number X the console reads, I have to calculate and print the sum of the first 5 consecutive even numbers (starting with the number X, if it’s a even number). Easy? Well, this was my solution (code in C, btw):

#include <stdio.h>

void main() {
	unsigned short int x = 0;
	
	printf("Insert a number: ");
	scanf("%hu", &x);
	
	while(x != 0) {
		unsigned short int sum = 0, counter = 0, end_loop = 0;
		
		while(end_loop < 5) {
			if(((x + counter) % 2) == 0) {
				sum = sum + x + counter;
				end_loop++;
			}
			
			counter++;
		}
		
		printf("The sum of the first 5 even numbers are : %hu\n\n", sum);
		
		printf("Insert a number: ");
		scanf("%hu", &x);
	}
}

But here it comes the “official” solution:

#include <stdio.h>

int main() {
	int x, sum;
	
	printf("Insert a number: ");
	scanf("%d", &x);
	
	while(x != 0) {
		if(x % 2 == 0)
			x = x + 1;        
		
		sum = 5 * x + 20;
		printf("SUM = %d\n", sum);
		
		printf("Insert a number: ");
		scanf("%hu", &x);
	}
}

Can you see it? Most of times I come with a “fancy” solution for something that is actually pretty simple… To be honest, this is very frustrating and makes me think if this is really for me… :pensive:

Honestly I don’t get how either of these worked. Why are you adding the counter to the sum again?

Your description can be understood to be that if you get the following numbers:

2
4
7
8
9
10
12

Then you must find the sum of 2,4,8,10 and 12

So not sure why you are adding the counter to that?

I’m sorry, I’m not a native english speaker so probably I messed up my explanation…

We have a loop, where the user inserts a number and the program has to return the sum of the first 5 consecutive even numbers after that number. For example, if the user insert the number 4, the first 5 even numbers are: 4, 6, 8, 10 and 12. In this case, since 4 is an even number, he must be included too. The sum of all those numbers are 40. This loops continues until the inserted number is equal to zero.

Actually, both codes works. The first one is mine, as you could see and the last one is the actual solution to the problem. Both code returns the same result. They booth works, but what makes me feel sad is my struggle to make simple algorithms. My solution is much more complicated than the last one…

I don’t think your code is that complicated, though I object to your use of a while loop instead of a for loop to add up the even numbers.

The ‘official’ doesn’t use a loop because it makes use of a formula for that task.

i tried to compile the ‘good’ version and it wouldn’t even compile.

c-practice/ $ make sumEven5
sumEven5.c:17:16: error: format specifies type ‘unsigned short *’ but the argument has type ‘int *’ [-Werror,-Wformat]
scanf(“%hu”, &x);
~~~ ^~
%u
1 error generated.
make: *** [: sumEven5] Error 1

Then after I fixed the error. I tried to run it and I got absolutely wrong results:
c-practice/ $ ./sumEven5
Insert a number: 5
SUM = 45
Insert a number: 10
SUM = 75
Insert a number: 10
SUM = 75
Insert a number: 10
SUM = 75
Insert a number: 10
SUM = 75
Insert a number: 10
SUM = 75
Insert a number: 0

I’m sorry I just don’t understand what the algorithm is meant to do.
However, I saw Jeremy commented on your coding style so I hope you are satisfied with that comment. (meanwhile, I will go and have my head examined…)

Side note - don’t mess around with short ints unless you really, really need to.

Probably I have mistyped something…

Btw I’m using GCC…

okey dokey. I can’t say that makes me happy. But I’m glad you got some feedback anyway.

Thanks for the advices, I’ll keep them in note…

Also, this may sound silly, but the problem is that I just can’t came up with some simple solution like the second code… You said that mine’s not that complicated, but in the end, the second solution is way simpler and “clean”, I guess. That’s exactly what frustrates me… I wish I could think in a simple solution like the ‘official’, but I couldn’t… and it’s because of that sometimes I think I don’t have sufficient skills to be a programmer…

how long have you been coding?
Since you are looking at other people’s solutions, take the time to note the differences.
Then try your solution again but this time, maybe use some syntactic sugar, or a for-loop or change things up. Coding is kind of like dressing up. It takes time to develop your own unique and flattering style (sorry I’m female so my similes skew that way sometimes)

1 Like

Thank you. I really appreciate that :smile: :smile:

Note: code still not working? Here it totally works, serious! :face_with_hand_over_mouth: :face_with_hand_over_mouth:

Making really tidy code takes a) experience and b) iteration.

Like I said, you have some strange style choices (while vs for on the inner loop) but the rest takes time and practice.

To be honest? Since I’m 10 :face_with_hand_over_mouth: :face_with_hand_over_mouth: my first contact with programming was with Pascal/Delphi… The thing is I’m not a IT professional, I have other degree and I’m returning into coding with a gap of years, maybe 10, at least, since I’m 28 now.

I’m returning into coding for fun, it’s a hobby of mine. The problem is I have this “compulsion” for write correct code, with minimum “bad practices”. I try to write the best code I can, but many times I come with a fancy, or unnecessary code, while there is a much simpler solution, and I just can’t figure how to write this simpler solution… it’s like: “How couldn’t i figure this out? it was obvious” while I’m looking into my “complicated” source code :anguished: :anguished:

Note: I’m doing the best I can to communicate with you. Hope you’re understanding me.

Oh, I forgot to explain that.

Jeremy, the reason I wrote the second ‘while’ loop it’s because I’m reviewing some C exercises and at the point of this particular exercise, the “for” and “do…while” loops have not yet been presented, only “while”… so, I tried to come up with some solution using only “while” loop, and that’s was the best solution I could think of…

I think you communicate well in a non-native language. (I am sure I couldn’t come to this level if I tried to do it in your language)
I just object to the weird code that doesn’t actually add anything correctly. But let’s not keep talking about it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.