#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Unable to get correct output


    Here's the code

    #include <stdio.h>
    #include "stdafx.h"

    int main()
    {
    int a1, a2, a3;
    int b1, b2, b3;
    int x1, x2, x3;
    int c;
    int d1, d2, d3;

    printf("Enter vector A:\n");
    scanf("%d%d%d", &a1, &a2, &a3);
    printf("Enter vector B:\n");
    scanf("%d%d%d", &b1, &b2, &b3);
    printf("Enter value of C:\n");
    scanf("%d", &c);

    //calculation part

    x1 = b1 * c;
    x2 = b2 * c;
    x3 = b3 * c;

    d1 = x1 + a1;
    d2 = x2 + a2;
    d3 = x3 + a3;

    printf("Vector D = %d %d %d", &d1, &d2, &d3);

    return 0;
    }

    Please help, thanks
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    As a newcomer, you need to be aware that the forum software does not by default retain indentation. Therefore, you need to put code samples in between [code] tags, like so:

    [code]
    code goes here.
    [/code]

    You can do this automatically with the '#' button at the top of the editing window, or the highlight marker button right next to it. As a courtesy to you, I have taken the liberty of putting your code in [highlight] tags for you:

    C Code:
    #include <stdio.h>
    #include "stdafx.h"
     
    int main()
    {
        int a1, a2, a3;
        int b1, b2, b3;
        int x1, x2, x3;
        int c;
        int d1, d2, d3;
     
        printf("Enter vector A:\n");
        scanf("%d%d%d", &a1, &a2, &a3);
        printf("Enter vector B:\n");
        scanf("%d%d%d", &b1, &b2, &b3);
        printf("Enter value of C:\n");
        scanf("%d", &c);
     
        //calculation part
     
        x1 = b1 * c;
        x2 = b2 * c;
        x3 = b3 * c;
     
        d1 = x1 + a1;
        d2 = x2 + a2;
        d3 = x3 + a3;
     
        printf("Vector D = %d %d %d", &d1, &d2, &d3);
     
        return 0; 
    }


    Now that we can read the code more easily, can you tell us just what isn't working for you? Just saying "it doesn't work" isn't enough information for us to go by; we need to know how it is failing. What output are you getting, and what were you expecting?

    On a guess, I would say the problem lies on line 29:
    Code:
        printf("Vector D = %d %d %d", &d1, &d2, &d3);
    Here, you are passing printf() three pointer values, rather than three int values as it is expecting, and as a result it isn't going to show the correct values. If you remove the ampersands, it should work correctly.

    As an aside, you could re-write this to use arrays and save a good deal of trouble:

    C Code:
    #include <stdio.h>
    #include "stdafx.h"
     
    int main()
    {
        int a[3];
        int b[3];
        int x[3];
        int c;
        int d[3];
        int i;
     
        printf("Enter vector A:\n");
        scanf("%d%d%d", &a[0], &a[1], &a[2]);
        printf("Enter vector B:\n");
        scanf("%d%d%d", &b[0], &b[1], &b[2]);
        printf("Enter value of C:\n");
        scanf("%d", &c);
     
        /* calculation part */
     
        for (i = 0; i < 3; i++)
        {
            x[i] = b[i] * c;
            d[i] = x[i] + a[i];
        }
     
     
        printf("Vector D = %d %d %d", d[0], d[1], d[2]);
     
        return 0;
    }
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Next
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,216
    Rep Power
    2222
    What happens when you read the warnings? Here's what I got (MinGW gcc):
    C:TEST>gcc -Wall fadzli.c
    fadzli.c: In function `main':
    fadzli.c:29: warning: int format, pointer arg (arg 2)
    fadzli.c:29: warning: int format, pointer arg (arg 3)
    fadzli.c:29: warning: int format, pointer arg (arg 4)

    C:TEST>
    Here's line 29:
    printf("Vector D = %d %d %d", &d1, &d2, &d3);
    The reason is because you tell printf that you're giving it integers when in fact you are giving it pointers to integers.

    Which is exactly what Schol-R-LEA told you, you self-entitled idiot. And the rest of what he told you is exactly what you need to do. So stop being an idiot and pay attention!


    Also, never ignore warnings. When you run a program that gives you warnings, you are running a broken program that will do unexpected things. Warnings are far more important than error messages are. Never ignore warnings.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo