> Help beginner in c programing need help with assignment?

Help beginner in c programing need help with assignment?

Posted at: 2014-12-18 
I agree with others who say you need to figure out how to do this yourself. Certainly your instructor thinks he has given you all the information you need, so you should be able to do it. I think you can learn a lot by looking at my code and figuring out what I did and why. It's self-explanatory, so I don't need to add anything more.

#include

#define MAX_STR_LEN 64

#define QUIT 0

int getInt(void);

typedef enum { false = 0, true } bool;

int main(int argc, char *argv[]) {

? ? int n, sumPos = 0, sumNeg = 0, cntPos = 0, cntNeg = 0;

? ? printf("Enter positive or negative integers, %d to quit:\n", QUIT);

? ? do {

? ? ? ? if ((n = getInt()) > 0) {

? ? ? ? ? ? sumPos += n;

? ? ? ? ? ? ++cntPos;

? ? ? ? } else if (n < 0) {

? ? ? ? ? ? sumNeg += n;

? ? ? ? ? ? ++cntNeg;

? ? ? ? }

? ? } while (n != 0);

? ? printf("sum of positive values = %d\n", sumPos);

? ? printf("sum of negative values = %d\n", sumNeg);

? ? printf("avg of positive values = %g\n", sumPos / (float)cntPos);

? ? printf("avg of negative values = %g\n", sumNeg / (float)cntNeg);

? ? return 0;

}

int getInt() {

? ? bool inputOk;

? ? static char line[MAX_STR_LEN];

? ? int z;

? ? printf("> ");

? ? for (inputOk = false; inputOk == false; ) {

? ? ? ? fgets(line, MAX_STR_LEN, stdin);

? ? ? ? inputOk = (sscanf(line,"%d",&z) == 1);

? ? ? ? if (inputOk == false) {

? ? ? ? ? ? printf("invalid input, try again: ");

? ? ? ? }

? ? }

? ? return z;

}

#if 0

Sample run:

Enter positive or negative integers, 0 to quit:

> 2

> xx

invalid input, try again: 5

> -3

> -5

> 4

> 0

sum of positive values = 11

sum of negative values = -8

avg of positive values = 3.66667

avg of negative values = -4

#endif

Just tested this working code in visual studio.

#include

#include

int main()

{

int positiveNum = 0;

int negativeNum = 0;

int uInput = 0;

printf("Enter a number: [Press zero to exit] ");

scanf_s("%d", &uInput);

while (uInput != 0){

if (uInput > 0){

positiveNum = positiveNum + uInput;

}

if (uInput < 0){

negativeNum = negativeNum + uInput;

}

printf("Enter a number: [Press zero to exit] ");

scanf_s("%d", &uInput);

}

printf("Negative Sum is: %d\n", negativeNum);

printf("Positive Sum is: %d\n", positiveNum);

return (0);

}

How does it work: Declare variables, ask user for number, test if number is zero, if not enter the while loop, if the value is greater than zero add to postitiveNum variable, if it's less than zero add to negativeNum variable, ask user for input again. When loop exits, display results.

Asking people to do your homework isn't going to go well. Break it down into parts. That describes what you need to do relatively clearly. You just need to split the steps up and you'll see it.

Here's a hint. You only need to use addition, counter variables, and a few if statements. A loop of some sort too. It's pretty simple.

Lazy, Lazy, lazy.

Write the pseudo code and then convert into real code.

scanf ("%d ", &input) until (zero) {

int sumPositive = add (positive, all, &posknt);

int sumNegative = add (negative, all, %negkint);

printf ("%d %d %f \n", sumPostive, sumNegative, sumPositive / poskint,, sumNegative / negknt);

}

Teacher gave us an assignment and let me just say he has a horrible accent and to add to that, he doesnt explain anything well.

heres the assignment

Write a complete C program to do the following:

There is a need for program that adds all positive number and place them in sumPositive

and sum of negative numbers and place them in sumNegative . At the end program should

prints the values in sumPositive and sumNegative and also compute average of positive

and negative numbers. The program should end when user type zero.

now I would really appreciate it if someone could write the program and then explain how you did it I know I am asking for a lot but please help me out :)

I understand the basics also I am using notepad ++