Skip to contentSkip to conversation

C program help

DISCUSSION spiros85 5 participants 5 messages Sep 19, 2009 – Dec 29, 2009

lore
Share & tools
lore

spiros85

author
#0 lore raw

Hi guys of dear forum!I am a new programmer in C programming language and i have a question for you about the following C code:
#include <stdio.h>
#include <stdlib.h>

int main()

{
    int x;
    int answer;
    int right;
    int wrong;
    printf("This is a program of sum\n");
    right = 0; /*Initialization of right value*/
    wrong = 0; /*Initialization of wrong value*/
    for(x = 1; x <= 10; x++)[QUOTE][/QUOTE]
    {
          printf("What is %d + %d: ", x, x);
          scanf("%d", &answer);
          printf("The sum is %d\n", answer);
          printf("\a");
          if(answer == x + x)
          {
             printf("%d is RIGHT answer\n", answer);
             right++; /*Saves and increases the amount of right answers*/
          }
          else
          {
             printf("%d is WRONG answer\n", answer);
             b[10] = answer;
             wrong++; /*Saves and increases the amount of wrong answers*/
             printf("The correct answer is %d\n", x + x);
          }
    }
    printf("The right answers are %d and the wrong are %d\n", right, wrong);
    fflush(stdin);
    getchar();
}

First, I use DEV C++ compiler!This program asks from user to find the sum of 1+1,2+2 until for loop reaches 10.As you can observe with right++ and wrong++ i can define and print to the screen the amount of right or wrong numbers!
My question is how could I define the number of correct and wrong numbers in collaboration with their amount?For example:
The right answers are 8 and wrong are 2
The right answers are 2 4 6 8 10 12 14 16
The wrong answers are 22 44

Signature · 2 lines
View this message in context: http://www.nabble.com/C-program-help-tp25522429p25522429.html
Sent from the linux-c-programming mailing list archive at Nabble.com.
#1 lore raw

Not tested, but here's a simple (not very efficient or elegant, but reasonable) concept. It's basically keeping a history, and postprocessing the history to generate the lists. It might be better to keep separate right/wrong lists as you go, but it's not as simple...

#include <stdio.h>
#include <stdlib.h>
#define NPROBLEMS 10

int main()
{ int x[NPROBLEMS], y[NPROBLEMS], right_answer[NPROBLEMS], given_answer[NPROBLEMS];
int nright, nwrong;
int i;

  for (i = 0; i < NPROBLEMS; ++i)
  { x[i] = i;
    y[i] = i;
    right_answer[i] = x[i] + y[i];
    printf("What is %d + %d? ", x[i], y[i]);
    scanf("%d", &(given_answer[i]));
    if (given_answer[i] == right_answer[i])
    { printf("%d is RIGHT answer\n", given_answer[i]);
      ++nright;
    }
    else
    { printf("%d is WRONG answer; the RIGHT answer is %d\n", given_answer[i], right_answer[i]);
      ++nwrong;
    }

}

  printf("You got %d right and %d wrong\n", nright, nwrong);

  printf("Right answers:");
  for (i = 0; i < NPROBLEMS; ++i)
    if (right_answer[i] == given_answer[i])
      printf(" %d", given_answer[i]);
  printf("\n");

  printf("Wrong answers:");
  for (i = 0; i < NPROBLEMS; ++i)
    if (right_answer[i] != given_answer[i])
      printf(" %d", given_answer[i]);
  printf("\n");

return 0;
}

Quoted 52 lines · On 09/19/2009 08:13 -0700, spiros85 wrote:
Quoted 52 lines
	
	Hi guys of dear forum!I am a new programmer in C programming language and i
	have a question for you about the following C code:
	#include <stdio.h>
	#include <stdlib.h>
	
	int main()
	{
	    int x;
	    int answer;
	    int right;
	    int wrong;
	    printf("This is a program of sum\n");
	    right = 0; /*Initialization of right value*/
	    wrong = 0; /*Initialization of wrong value*/
	    for(x = 1; x <= 10; x++)[QUOTE][/QUOTE]
	    {
	          printf("What is %d + %d: ", x, x);
	          scanf("%d", &answer);
	          printf("The sum is %d\n", answer);
	          printf("\a");
	          if(answer == x + x)
	          {
	             printf("%d is RIGHT answer\n", answer);
	             right++; /*Saves and increases the amount of right answers*/
	          }
	          else
	          {
	             printf("%d is WRONG answer\n", answer);
	             b[10] = answer;
	             wrong++; /*Saves and increases the amount of wrong answers*/
	             printf("The correct answer is %d\n", x + x);
	          }
	    }
	    printf("The right answers are %d and the wrong are %d\n", right, wrong);
	    fflush(stdin);
	    getchar();
	}
	 
	First, I use DEV C++ compiler!This program asks from user to find the sum of
	1+1,2+2 until for loop reaches 10.As you can observe with right++ and
	wrong++ i can define and print to the screen the amount of right or wrong
	numbers!
	My question is how could I define the number of correct and wrong numbers in
	collaboration with their amount?For example:
	The right answers are 8 and wrong are 2
	The right answers are 2 4 6 8 10 12 14 16
	The wrong answers are 22 44
	-- 
	View this message in context: http://www.nabble.com/C-program-help-tp25522429p25522429.html
	Sent from the linux-c-programming mailing list archive at Nabble.com.
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

End of included message

Signature · 1 line
[email protected]
#2 lore raw
In reply to spiros85
Quoted 39 lines · On Sat, Sep 19, 2009 at 8:43 PM, spiros85 <[email protected]> wrote:

Hi guys of dear forum!I am a new programmer in C programming language and i have a question for you about the following C code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
   int x;
   int answer;
   int right;
   int wrong;
   printf("This is a program of sum\n");
   right = 0; /*Initialization of right value*/
   wrong = 0; /*Initialization of wrong value*/
   for(x = 1; x <= 10; x++)[QUOTE][/QUOTE]
   {
         printf("What is %d + %d: ", x, x);
         scanf("%d", &answer);
         printf("The sum is %d\n", answer);
         printf("\a");
         if(answer == x + x)
         {
            printf("%d is RIGHT answer\n", answer);
            right++; /*Saves and increases the amount of right answers*/ }
         else
         {
            printf("%d is WRONG answer\n", answer);
            b[10] = answer;
            wrong++; /*Saves and increases the amount of wrong answers*/ printf("The correct answer is %d\n", x + x);
         }
   }
   printf("The right answers are %d and the wrong are %d\n", right, wrong); fflush(stdin);
   getchar();
}

Quoted 9 lines

First, I use DEV C++ compiler!This program asks from user to find the sum of 1+1,2+2 until for loop reaches 10.As you can observe with right++ and wrong++ i can define and print to the screen the amount of right or wrong numbers!
My question is how could I define the number of correct and wrong numbers in collaboration with their amount?For example:
The right answers are 8 and wrong are 2
The right answers are 2 4 6 8 10 12 14 16
The wrong answers are 22 44

#include<stdio.h>
#define N 10

int main() {
	int answer;
	int i;
	int result[N][2];
	int right = 0, wrong = 0;

	for (i=1;i<=N;i++) {
		printf("What is %i + %i ? :",i,i);
		scanf("%d",&answer);
		if (i+i == answer) {
			result[i-1][0] = answer;
			result[i-1][1] = 1;
			right++;
		} else {
			result[i-1][0] = answer;
			result[i-1][1] = 0;
			wrong++;
		}
	}

	printf ("The right answers are %d and wrong answers are %d\n",right, wrong);
	printf("The right answers are : ");
	for (i=0;i<N;i++) {
		if (result[i][1]) {
			printf("%d ", result[i][0]);
		}
	}
	printf("\n");
	printf("The wrong answers are : ");
	for (i=0;i<N;i++) {
		if (!result[i][1]) {
			printf("%d ", result[i][0]);
		}
	}
	printf("\n");
	return 0;
}
Quoted 9 lines

--
View this message in context: http://www.nabble.com/C-program-help-tp25522429p25522429.html
Sent from the linux-c-programming mailing list archive at Nabble.com.

--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Signature · 6 lines
Thanks -
Manish
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
In reply to spiros85

here is solution

#include <stdio.h>
#include <stdlib.h>

int main()

{
    int x;
    int answer;
    int right;
    int wrong;
    int righta[100];
    int wronga[100];
    int i;

    printf("This is a program of sum\n");
    right = 0; /*Initialization of right value*/
    wrong = 0; /*Initialization of wrong value*/
    for(x = 1; x <= 10; x++)
    {
          printf("What is %d + %d: ", x, x);
          scanf("%d", &answer);
          printf("The sum is %d\n", answer);
          printf("\a");
          if(answer == x + x)
          {
             printf("%d is RIGHT answer\n", answer);
             righta[right]=answer;
             right++; /*Saves and increases the amount of right answers*/
          }
          else
          {
             printf("%d is WRONG answer\n", answer);
             wronga[wrong] = answer;
             wrong++; /*Saves and increases th      }
    }
    righta[right]=0;
    wronga[wrong]=0;
    printf("The right answers are %d and the wrong are %d\n", right, wrong);
    printf("The right answers are ");
    for(i=0;righta[i];i++){
        printf("%d\t",righta[i]);
    }
    printf("\nThe wrong answers are ");
    for(i=0;wronga[i];i++){
        printf("%d\t",wronga[i]);
    }
    fflush(stdin);
    getchar();
}

e amount of wrong answers*/

             printf("The correct answer is %d\n", x + x);
Quoted 50 lines · spiros85 wrote:

Hi guys of dear forum!I am a new programmer in C programming language and i have a question for you about the following C code:
#include <stdio.h>
#include <stdlib.h>

int main()

{
    int x;
    int answer;
    int right;
    int wrong;
    printf("This is a program of sum\n");
    right = 0; /*Initialization of right value*/
    wrong = 0; /*Initialization of wrong value*/
    for(x = 1; x <= 10; x++)[QUOTE][/QUOTE]
    {
          printf("What is %d + %d: ", x, x);
          scanf("%d", &answer);
          printf("The sum is %d\n", answer);
          printf("\a");
          if(answer == x + x)
          {
             printf("%d is RIGHT answer\n", answer);
             right++; /*Saves and increases the amount of right answers*/
          }
          else
          {
             printf("%d is WRONG answer\n", answer);
             b[10] = answer;
             wrong++; /*Saves and increases the amount of wrong answers*/
             printf("The correct answer is %d\n", x + x);
          }
    }
    printf("The right answers are %d and the wrong are %d\n", right,
wrong);
    fflush(stdin);
    getchar();
}

First, I use DEV C++ compiler!This program asks from user to find the sum of 1+1,2+2 until for loop reaches 10.As you can observe with right++ and wrong++ i can define and print to the screen the amount of right or wrong numbers!
My question is how could I define the number of correct and wrong numbers in collaboration with their amount?For example:
The right answers are 8 and wrong are 2
The right answers are 2 4 6 8 10 12 14 16
The wrong answers are 22 44

Signature · 2 lines
View this message in context: http://www.nabble.com/C-program-help-tp25522429p25820023.html
Sent from the linux-c-programming mailing list archive at Nabble.com.
#4 lore raw
In reply to spiros85

1) SEE , wat i feel is that you can have a count after u answer
for the question u ask using printf statement... that count must be initilized to 0 which will act as a question number. .
2) And since u are a beginner in C , iam suggesting u to make use
of two arrays where u can have one for right answers and other for wrong answers....
3) In the array of right answers where u are checking ur answer
== x+x , inside that statement assign the count to the right answerd array as:
right[i]=count;
4) if ur if becomes false , in else same u must do as above but
that should be :

                              wrong[j]=count;

after this u would get ur desired output....... any doubts , mail me :[email protected]
thank u

Signature · 2 lines
View this message in context: http://old.nabble.com/C-program-help-tp25522429p26951151.html
Sent from the linux-c-programming mailing list archive at Nabble.com.