Thread: Wrong variable value assigned

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Code:
    #include <stdio.h>
    
    
    int main()
    {
     int n,a;
     int calc,sum,x,i,final_a;
     while(scanf("&#37;d %d",&n,&a) == 2)
         {
          sum=0;
          printf("n:%d -- ",&n);
          for(i=1; i<=n; i++)
                    {
                     calc = 0;
                     for(x=1;x<i;x++)
                        {
                         final_a = a;
                         final_a = final_a * a;
                         
                        }
                     calc = i * final_a;
                     sum = sum + calc;
                    }
          printf("%d\n",sum);
         }
    }
    No variable prints out correctly :S

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember: printf takes a value, scanf takes an address. So while you do
    Code:
    scanf("%d", &n);
    you do
    Code:
    printf("%d", n);

  3. #18
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Aha. What a stupid mistake sorry about that.
    Hey now I finally got the program working 100&#37;.
    Code:
    #include <stdio.h>
    
    int main()
    {
     int n,a;
     int calc,sum,x,i,final_a;
     while(scanf("%d %d",&n,&a) == 2)
         {     
          sum = 0;
          for(i=1; i<=n; i++)
                    {
                     final_a = a; 
                     if(i == 1)
                          {
                               
                          }
                     else
                     {  
                     for(x=1;x<i;x++)
                        {
                         final_a = final_a * a;                    
                        }
                     }
                     calc = i * final_a;
                     sum = sum + calc;
                    }
          printf("%d\n",sum);
         }
    }
    But when I submit it to online judge acm; it always says runtime error. The code compiles and runs (with input file and output as well) on a unix box no problems whatsoever. So anybody got any ideas?

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Nevermind. I found my own error!
    I had to add "int argc, char* argv[]" and "return 0;" to my code for it to compile with no warnings.
    Thanks guys for all the help!!!

  5. #20
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Maybe you have interpreted the formula wrongly, except from what tabstop said.
    You are calculating: SUM(i = 1:N) of ( SUM(j = 1:i-1)*i) which with a little breaking down becomes:
    SUM(i = 1:N) of ( (i^2) * (i-1) )/2 which is a 4th degree polynomial over N.
    Is that what you want to compute?
    Also maybe 'x' should go up to and including 'i' in the inner loop?
    Last edited by xuftugulus; 02-29-2008 at 11:03 AM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                     if(i == 1)
                          {
                               
                          }
                     else
    Looks a bit overcomplicated to me.

    Perhaps you need to return 0 from main?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    yuh well now im just confused...
    here is my final code that i submitted to the judge and got a "wrong answer" reply to...
    Code:
    /* CSC1140@ 07611201 74kzYkXl 10523 */
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
     int n,a;
     int calc,sum,x,i,final_a,count=0;
     while(scanf("&#37;d %d",&n,&a) == 2)
         {     
          count++;
          sum = 0;
          for(i=1; i<=n; i++)
                    {
                     final_a = a; 
                     if(i == 1)
                          {
                               
                          }
                     else
                     {  
                     for(x=1;x<i;x++)
                        {
                         final_a = final_a * a;                    
                        }
                     }
                     calc = i * final_a;
                     sum = sum + calc;
                    }
          if(count == 1)
          {
              printf("%d",sum);
          }
          else
          {
              printf("\n%d",sum);
          }
         }
         return 0;
    }
    i know its probably over-complicated; but it was all i could think of to make sure there were no extra "\n" at the end of output.
    I tested it for many different inputs and the output is correct each and every time...

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I might be mistaken, but I think you're supposed to have a new-line character at the end of the input. (IOW, I wouldn't bother with the if, but just print "%d\n" each time.) The other stuff looks over-complicated, but correct. (E.g., the if (i==1) test is unnecessary, since if i was 1, the for-loop would never actually happen.)

  9. #24
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    At first I left the code without the if statement at the very end (the one that gets rid of the last \ n) but I got wrong answer as well. Im pretty sure my calculations are all correct. But you don't see anything wrong with the code do you?

    Thanks

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only other thing I can think of offhand is overflow; don't know if you know limits on how large your numbers are supposed to get.

  11. #26
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    N and A (integer, 1<=N<=150 & integer, 0<=A<=15)
    Does that help?

  12. #27
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    15^150 will definitely overflow your integer final_a var
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #28
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Aha.
    Yes numbers for n and a that are getting to the highest do cause negative answers (which i assume means overflow)...
    I included the extra header; it helped edge the overflow to a larger number. I then tried unsigned; but same results.

  14. #29
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    2.5923214794879449459448544681805e+176 - I suppose you need double
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #30
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Thanks for all the help guys.. But I think I am finally going to give up on this problem...
    I mean I solved the overflow problem and made sure the program worked for those range of numbers... everything works now and displays exactly as it should... I tried it with and without the endline at the end... but all I get are "Wrong Answers".
    Again thank you for all of your help and I have learned ALOT from this post!
    But if you do think of why my code would produce a wrong answer; please let me know. Id love to learn why so i dont make that mistake again...
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to set pointer for environment variable
    By spiky1 in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2006, 05:19 PM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM