Thread: Need help with array calculation

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah well that error does make sense to me.

    Try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      //Set store tax variables
      float StoreTax1;
      //Declare ProductPrice array
      float ProductPrice[] = {4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
      float *Store1TaxPerItem;
      float Store1;
      int x = 0;
    
      StoreTax1 = 0.0725f;
      Store1TaxPerItem = malloc(sizeof(ProductPrice));
      Store1 = 0.0f;
    
      if(!Store1TaxPerItem)
      {
        fputs("Uh oh, spighetti-o! Out of memory! Please go purchase more.\n", stderr);
        return EXIT_FAILURE;
      }
    
      printf("Begin process");
    
      for (x = 0; x < sizeof(ProductPrice)/sizeof(*ProductPrice); x++ )
      {		
        Store1TaxPerItem[x] = ProductPrice[x] * StoreTax1;
        printf("The result Store1TaxPerItem &#37;f\n", Store1TaxPerItem[x]);
    
        Store1 = Store1 + Store1TaxPerItem[x];
      }
      printf("The result is %f\n", Store1);
      return EXIT_SUCCESS;
    }

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    FYI, you have to declare all variables at the beginning of a code block in C. That is why the compiler error "made sense to me."

  3. #18
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by master5001 View Post
    FYI, you have to declare all variables at the beginning of a code block in C. That is why the compiler error "made sense to me."
    So C is not as forgiving as VB in terms of where you declare your variables. I'll keep this in mind.

    I have a new error .
    Code:
    line 18: ! only for int
    I removed the exclamation point and rebuilt it and it ran. However like before I did not get my results shown.

    Thanks for your patience.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    ! only for int? I don't think I've ever seen that message, and I'm pretty sure that that's not conforming -- the C standard says that ! applies to scalar types, and that includes all arithmetic types and all pointer types. Maybe the compiler is trying to save you from yourself; what compiler are you using?

    Anyway the equivalent would be if(Store1TaxPerItem!=NULL).

  5. #20
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by tabstop View Post
    ! only for int? I don't think I've ever seen that message, and I'm pretty sure that that's not conforming -- the C standard says that ! applies to scalar types, and that includes all arithmetic types and all pointer types. Maybe the compiler is trying to save you from yourself; what compiler are you using?

    Anyway the equivalent would be if(Store1TaxPerItem!=NULL).
    I'm using the miracle C compiler.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by magiccity77 View Post
    I'm using the miracle C compiler.
    Miracle C was the result of a student's homework assignment. You'll want to find a more Standard's compliant compiler. You can find good, free replacements here.

  7. #22
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by citizen View Post
    Miracle C was the result of a student's homework assignment. You'll want to find a more Standard's compliant compiler. You can find good, free replacements here.
    Thanks I'll this link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM