Hello,
ok here are some suggestions so you can can become better at programming.
- I do not like to declare variables in a single line because it's hard to read but that it just my prefference. When I change it to a per line basis this shows up:
Code:
int num[1000];
int sum = 0;
int i = 0;
int j = 0;
int numDigits;
int counterDividible = 0;
int counterDigits = 0;
Such a big code of variables might tell you that the procedure is trying to do a lot. This bing us to the other section.
- Write pseudocode it will guide you so you do not end up with a huge single procedure and you devide it into meaningfull parts. Lets get the pseudocode for your task(s)
1. Enter numbers until 0 or -1
2. Get sum of all positive and 2 digit numbers
3. Get the count of divisible by 3.
This pseudocode helped us correctl identify several functions:
Code:
int isTwoDigitNumber(int number);
int isDivisibleByThree(int number);
- Use constants and enumerators to make your whole code read easily
Code:
enum {NO_ERRORS, ERROR};
enum {FALSE, TRUE};
Finally after all this we have something like the code below. I have left the function implementations for you to do.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* constants and enumerators */
enum {NO_ERRORS, ERROR};
enum {FALSE, TRUE};
/* function prototypes */
int isTwoDigitNumber(int );
int isPossitive(int );
int isDivisibleByThree(int );
int main(void) {
/* Enter numbers until you enter 0 or -1
and print sum of all positive two-digit numbers entered
and print how many of these numbers divisible by 3 */
int sum = 0;
int number = 0;
int counterDivisible = 0;
int leave = FALSE;
while (!leave){
scanf ("%d", &number);
if (number == 0 || number == -1){
leave = TRUE;
}else{
if (isPossitive(number) && isTwoDigitNumber(number)){
sum += number;
}
if (isDivisibleByThree(number)){
counterDivisible++;
}
}
}
printf("Sum: %d \n Divisible by 3:%d", sum , counterDivisible);
return NO_ERRORS;
}
/* checks if number has 2 digits then TRUE else FALSE */
int isTwoDigitNumber(int number){
}
/* checks if number is possitive then TRUE else FALSE */
int isPossitive(int number){
}
/* checks if number is divisisible by 3 then TRUE else FALSE */
int isDivisibleByThree(int number){
}
Hope this helps