Im new and need help

This is a discussion on Im new and need help within the C Programming forums, part of the General Programming Boards category; Hi everybody! Im an absolute beginner in c coding. I still have problems with dyn allocation and pointers. could somebody ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    22

    Im new and need help

    Hi everybody!

    Im an absolute beginner in c coding. I still have problems with dyn allocation and pointers.

    could somebody help me with this? It calculates the skalar product of 2 vectors a and b with n dimensions.
    cant get this to work.
    Code:
                                 /* Skalarproduct dyn alloc. 14.2 */
    #include <stdio.h>
    #include <stdlib.h>
    void main()
    {
       int n, j, i;          /* n=dimension*/
       float *a, *b, skaprod=0.0;
       printf("Enter dimension of the vector?-->");
       scanf("%d", &n);
       a= (float*)malloc(n*sizeof(float));
       b= (float*)malloc(n*sizeof(float));
       for (i=0; i<n; i++)
       {
           printf("a[%d]=",i);
           scanf("%d",&a[i]);
       }
       for(j=0; j<n; j++)
       {
           printf("b[%d]=",j);
           scanf("%f",&b[j]);
       }
       for(i=0;i<n;i++)
          skaprod=skaprod+(a[i]*b[i]);
       printf("the result is-->%f\n",skaprod);
       free(a);
       free(b);
    }
    my compiler is lcc-win32 and I think its not easy to handle for beginners.
    Last edited by staticalloc; 08-31-2004 at 11:54 AM.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Welcome to the boards!

    I believe "%d" needs to be "%f" in the first scanf function for it to read in correctly.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    I think jverkoey pointed out what needs to be fixed. Here's a suggestion for future reference:

    When you are running a new program and it "almost works". Like, in this case the program runs to completion, but has the minor problem that it gives unexpected results (sometimes known as the wrong answer), put in printf() statements after your inputs.

    For example,

    Code:
       printf("\nYou entered the following values:\n");
       for (i = 0; i < n; i++) {
         printf("a[%d] = %f\n", i, a[i]);
       }
       printf("\n");
       for (i = 0; i < n; i++) {
         printf("b[%d] = %f\n", i, b[i]);
       }
       printf("\n");
       printf("the result is-->%f\n",skaprod);
    You could see that wrong values being used for a[], and you would know where to look.


    Remember, printf() is your friend.

    This is a good-looking effort if you really are a beginner.

    Good luck!

    Dave
    Last edited by Dave Evans; 08-31-2004 at 12:58 PM.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    22
    thanks for the advice and help.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    void main() is bad and I'm surprised you weren't already scolded for it.

    Also, you should check to make sure malloc() was successful (if(a == NULL) { ... }) and don't type cast the return value of malloc() in C.

    But not a bad job for a beginner
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    22
    1. void main() is bad? Am I suppose to use int main(){return 0;} ?
    2. type cast is no no in C? I thought without type casting, malloc will return a pointer to void by default?

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    About #1, it's kind of a taboo here at the forums to dance around with your arms flailing around screaming at people who use void main.

    If you do a search for "void main" you'll find a lot of posts about it, no use re-explaining it here.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by staticalloc
    2. type cast is no no in C? I thought without type casting, malloc will return a pointer to void by default?
    In C void * is automatically converted to whatever pointer type to are assigning it to. So if you are assigning the void * to a char * it converts it for you. Casting the return of malloc has a nasty problem of hiding the fact that you might have forgotten to #include <stdlib>

  9. #9
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Quote Originally Posted by Thantos
    Casting the return of malloc has a nasty problem of hiding the fact that you might have forgotten to #include <stdlib>
    You mean #include <stdlib.h>
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Some things to read:

    void main
    casting malloc
    descriptive subject lines
    getting a number from the user

    >>printf("Enter dimension of the vector?-->");
    After doing this, use fflush(stdout); to force the message to the screen. If you don't, the message might not be displayed, leaving the user wondering what the program is waiting for.

    And lastly, welcome.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21