Page 1 of 9 1234 ... LastLast
Results 1 to 10 of 82
  1. #1

    Default My C program - Help me in identifying anything wrong with it


    Guys, naa miy assignment sa among IT class. Ang question kay:

    "Create a pseudocode, flowchart and C program that ask the user 2 integer numbers. The user then enters another character in order to choose which operator to perform: A - addition, S - subtraction, M - multiplication, D- division. Create separate functions for each operation."

    Sample run:
    Enter two integers: 10,5
    Choices:
    (A) Addition
    (S) Subtraction
    (M) Multiplication
    (D) Division
    Choice: M
    The product is 50

    Mao ni ang code nga nahimo nako. Nagdiscuss na man mi sugod sa mga control statements, and naa pa mi sa IF controlled loop. The program ran perfectly sa Turbo C diri sa balay namo pero dili siya mu print sa choice and sa answer.

    Code:
    #include<stdio.h>
    #include<conio.h>
    int add(int, int);
    int subtract(int, int);
    float divide(int, int);
    int multiply(int, int);
    void main(void)
    {
     int a, b, d, e, f;
     float g;
     char c;
     clrscr();
     printf("Enter two integers separated by a space:\n");
     scanf("%d %d", &a, &b);
     printf("Actions:\n(A) Add\n(S) Subtract\n(D) Divide\n(M) Multiply\n");
     printf("Choice:\n");
     scanf("%c", &c);
     if (c=='A')
     {
      d=add(a, b);
      printf("The sum of %d and %d is %d", a, b, d);
      getch();
     }
     if (c=='S')
     {
      e=subtract(a, b);
      printf("The difference of %d and %d is %d", a, b, e);
      getch();
     }
     if (c=='D')
     {
      float e;
      g=divide(a, b);
      printf("The quotient of %d and %d is %d", a, b, g);
      getch();
     }
     if (c=='M')
     {
      f=multiply(a, b);
      printf("The product of %d and %d is %d", a, b, f);
      getch();
     }
     getch();
    }
    int add(int a, int b)
    {
     return (a+b);
    }
    int subtract(int a, int b)
    {
     return (a-b);
    }
    int multiply(int a, int b)
    {
     return (a*b);
    }
    float divide(int a, int b)
    {
     return ((float)a/b);
    }
    (PS: I don't know if this is allowed here sa forums. I just don't have anyone to guide me what's wrong with my code.. Check lang ko ni nga thread later sa gabii kay magclase sa ko )

    Thanks!

  2. #2
    bai. pwede rmn xguro wala ni xla
    int add(int a, int b)
    {
    return (a+b);
    }
    int subtract(int a, int b)
    {
    return (a-b);
    }
    int multiply(int a, int b)
    {
    return (a*b);
    }
    float divide(int a, int b)
    {
    return ((float)a/b);
    }

    then drtso nlng qa sa imung statements bah nga naai opertaion
    then if else para sa imung division para sa katung divisor ug dividen
    then imung multiplcation kai if else pd para sa kanang if mu input ug 0

  3. #3
    isulod mn tingale imo mga functions like add, subtract, etc... sa main function.

    imo kay...
    Code:
    void main(void) {
    ...
    }
    int add(int a, int b) {
    ...
    }

    isulod cguro sa main imo mga functions...
    Code:
    void main(void) {
       int add(int a, int b){
          //your code here
       }
    }

  4. #4
    Quote Originally Posted by xiao_xiao View Post
    isulod mn tingale imo mga functions like add, subtract, etc... sa main function.

    imo kay...
    Code:
    void main(void) {
    ...
    }
    int add(int a, int b) {
    ...
    }
    isulod cguro sa main imo mga functions...
    Code:
    void main(void) {
       int add(int a, int b){
          //your code here
       }
    }
    Java mana nga style sa coding bro...
    Ok ra ang pagkahimutang sa mga functions sa code sa TS.

  5. #5
    murag mao mn gud to akong nahinumduman kitong ga csc101 ko sauna... isulod tanan sa main functions... especially sa mga old compilers like turbo/borland c/c++... dli ko sure nuon. hehe

    or probably daghan kaayo ka ug getch...

    ani ra cguro...

    Code:
    void main(void) {
    
       if (c=='A')
       {
         d=add(a, b);
         printf("The sum of %d and %d is %d", a, b, d);     
       }
       
       getch();
    }
    pwede ra mn cguro sa last part ra ka mag getch();

  6. #6
    Quote Originally Posted by marcdaven View Post
    Guys, naa miy assignment sa among IT class. Ang question kay:

    "Create a pseudocode, flowchart and C program that ask the user 2 integer numbers. The user then enters another character in order to choose which operator to perform: A - addition, S - subtraction, M - multiplication, D- division. Create separate functions for each operation."

    Sample run:
    Enter two integers: 10,5
    Choices:
    (A) Addition
    (S) Subtraction
    (M) Multiplication
    (D) Division
    Choice: M
    The product is 50

    Mao ni ang code nga nahimo nako. Nagdiscuss na man mi sugod sa mga control statements, and naa pa mi sa IF controlled loop. The program ran perfectly sa Turbo C diri sa balay namo pero dili siya mu print sa choice and sa answer.

    Code:
    #include<stdio.h>
    #include<conio.h>
    int add(int, int);
    int subtract(int, int);
    float divide(int, int);
    int multiply(int, int);
    void main(void)
    {
     int a, b, d, e, f;
     float g;
     char c;
     clrscr();
     printf("Enter two integers separated by a space:\n");
     scanf("%d %d", &a, &b);
     printf("Actions:\n(A) Add\n(S) Subtract\n(D) Divide\n(M) Multiply\n");
     printf("Choice:\n");
     scanf("%c", &c);
     if (c=='A')
     {
      d=add(a, b);
      printf("The sum of %d and %d is %d", a, b, d);
      getch();
     }
     if (c=='S')
     {
      e=subtract(a, b);
      printf("The difference of %d and %d is %d", a, b, e);
      getch();
     }
     if (c=='D')
     {
      float e;
      g=divide(a, b);
      printf("The quotient of %d and %d is %d", a, b, g);
      getch();
     }
     if (c=='M')
     {
      f=multiply(a, b);
      printf("The product of %d and %d is %d", a, b, f);
      getch();
     }
     getch();
    }
    int add(int a, int b)
    {
     return (a+b);
    }
    int subtract(int a, int b)
    {
     return (a-b);
    }
    int multiply(int a, int b)
    {
     return (a*b);
    }
    float divide(int a, int b)
    {
     return ((float)a/b);
    }
    (PS: I don't know if this is allowed here sa forums. I just don't have anyone to guide me what's wrong with my code.. Check lang ko ni nga thread later sa gabii kay magclase sa ko )

    Thanks!
    Naa ra diri imo sayop TS:

    input:
    Enter two integers: 10,5
    wrong implementation:
    Code:
    printf("Enter two integers separated by a space:\n");
     scanf("%d %d", &a, &b);
    Dili masulod sa a and b variables ang 10 ug 5 ani imo implementation.

    To simplify, why won't you let user to input 1st number then scanf, then second number then scanf?
    Input:
    Enter first integer: 10
    Enter 2nd integer: 5

    Implementation:

    Code:
    printf("Enter first integer:\n");
    scanf("%d", &a);
    printf("Enter second integer:\n");
    scanf("%d", &b);
    If you really want to implement this input: "Enter two integers: 10,5", then you have to store this input into string which is declared as char* then you have to parse that string to get 10 and 5 separately from the string which I think complicated pa sa imo part since wala pamo naabot ana nga level sa inyo topic karon. Better use what I suggested to simplify things for you.

  7. #7
    hmmm... i think it's because ga comma ka sa imong input..
    it should be 10 5... not 10, 5 because sa imong scanf kay walay comma ang "%d %d" nimo

  8. #8
    sa imong sample run ga comma ka
    Sample run:
    Enter two integers: 10,5
    sa imong code kay ga ingon ka nga seperated by space dapat
    Code:
    printf("Enter two integers separated by a space:\n");

  9. #9
    Gi try nako ang imong program ari siya ma sangit, kay nangutana ka daan sa value for int a and b, then dili ma sulod.

    Naa koy lain nga gi program nako try lang nako if pwedi ni nimo.

    Code:
    #include<stdio.h>
    int sum(void);
    int diff(void);
    int prod(void);
    int quot(void);
    
    main(){
        char ch;
        do{
        clrscr();
        puts("a. Addition");
        puts("b. Subtraction");
        puts("c. Multiplication");
        puts("d. Division");
        puts("e. Exit Program");
        printf("\nSelect Choice (a,b,c,d,e): ");
        scanf("%c",&ch);
        if (ch=='a') printf("\nSum = %d",sum());
        else if (ch=='b') printf("\nDifference = %d",diff());
        else if (ch=='c') printf("\nProduct = %d",prod());
        else if (ch=='d') printf("\nQuotient = %d",quot());
        getch();
        }
        while (ch!='e');
    }
    
    
    int sum(void){
        int num1,num2,total;
        printf("\nEnter the first number  : "); scanf("%d",&num1);
        printf("Enter the sencond number: "); scanf("%d",&num2);
        total = num1 + num2;
        return total;
    }
    int diff(void){
        int num1,num2,total;
        printf("\nEnter the first number : "); scanf("%d",&num1);
        printf("Enter the second number: "); scanf("%d",&num2);
        total = num1 - num2;
        return total;
    }
    int prod(void){
        int num1,num2,total;
        printf("\nEnter the first number : "); scanf("%d",&num1);
        printf("Enter the second number: "); scanf("%d",&num2);
        total = num1 * num2;
        return total;
    }
    int quot(void){
        int num1,num2,total;
        printf("\nEnter the first number: "); scanf("%d",&num1);
        printf("Enter the second numer: "); scanf("%d",&num2);
        total = num1 / num2;
        return total;
    }
    Last edited by SmaRkieS; 02-11-2010 at 01:57 PM.

  10. #10
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    PHP Code:
    #include <stdio.h>

    int main(int argcchar *argv[])
    {

        
    int ab;

         
    printf("Enter two integers separated by a space: ");
         
    scanf("%d %d", &a,&b);
        
    printf("%d, %d\n"a,b);

        return 
    0;



    OUTPUT:
    Enter two integers separated by a space: 15 77
    15, 77
    Press any key to continue...

  11.    Advertisement

Page 1 of 9 1234 ... LastLast

Similar Threads

 
  1. Help me in my laptop problem
    By Wenxp7 in forum Computer Hardware
    Replies: 16
    Last Post: 10-08-2010, 07:48 PM
  2. pls help me in my pldt dsl connection...
    By glenntacan in forum Networking & Internet
    Replies: 5
    Last Post: 01-23-2009, 05:15 PM
  3. help me in my front panel config
    By absimon in forum Computer Hardware
    Replies: 5
    Last Post: 12-05-2008, 07:18 PM
  4. Please help me in martial arts.
    By Chalil in forum Sports & Recreation
    Replies: 11
    Last Post: 09-20-2006, 10:52 PM
  5. Where can I repair my flash drive?!!! Help me!!!
    By jonmlas in forum Gizmos & Gadgets (Old)
    Replies: 13
    Last Post: 11-30-2005, 12:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top