Thread: Help pleaasee!!

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    15

    Post Help pleaasee!!

    Can someone help me out its a homework. I wrote the program just stuck.


    1)Write a program that will ask the user to enter a number. Then createa loop of your choice that will multiply the number by itself thenumber of times equal to the number itself. Like the power functionin math x raised to the y power. Do this with a loop, not the mathpower function.


    my program

    Code:
    
    
    Code:
    #include <stdio.h>
    
    
    int main()
    
    
    {
    int a; 
    int x;
    
    
    printf("Enter a Number\n");
    fflush(stdin);
    scanf("%d", & a);
    
    
    for (int i=0; i<=x; i++)
        a= a*a;
      {
          printf("The result is %d \n",a);
      }  
      
    }
    which ever number i pick the program its suppose to multiply it that many times so if i pick 3 its gonna do 3x3x3

    my program i wrote whats to multiply any number i put in to the 4th power. can someone help me out and guide me the right way please and thank you.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you're missing one line; put x = a; before the loop.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    If i add x= a; any number i put in it will give me a 0

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    fflush has nothing to do with the loop not working. but thank you.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's all a step in the right direction. You may have simply gotten 0 because a happened to be initialized to 0. Something I missed was that the way a was multiplied, you would end up squaring the previous answer, so a would grow like 3, 9, 81, 6,561. Once you initialize a to 1 and multiply by the input only, it works better.

    I ended up rewriting it with better variable names, along with using the other advice.
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int value;
        int count;
        int product = 1;
        int c;
        
        printf("Enter a Number\n");
        scanf("%d", &value);
        while ((c = getchar()) != '\n' && c != EOF)
            ; // intentionally nothing
        
        for (count = value; count > 0; --count)
            product *= value;
        
        printf("The result is %d\n", product);
    }
    
    
    C:\Users\jk\Downloads>gcc -Wall --std=c99 foo.c
    
    
    C:\Users\jk\Downloads>a.exe
    Enter a Number
    3
    The result is 27
    The idea is the same.
    Last edited by whiteflags; 03-14-2017 at 03:13 PM.

  7. #7
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    Hey whiteflags do you have a personal email address. If you could answer some simply questions from me if you don't mind about programing i don't get any of this . thank you.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Please post on the forum if you want to talk to me.

  9. #9
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    Code:
    #include <stdio.h>      
    main ( )
    {
        int i, num[6];
        int oddNumbers = 0,evenNumbers = 0;
        for(i=0; i<=5; i++)     
        {
        printf("Enter a number betweem -20 and + 20 \n");
        fflush(stdin);
        scanf("%d",&num[i]);
        if (num [i] <-20 || num [i] > 20  )      
        {
        printf("number you entere outside the limits");
        i=i -1;}
         }  
         printf("\n Even numbers are:\n");
            for (i=0;i< 6; i++)     
        {
           if (num[i] % 2 == 0)
                   {
                       printf("%d\n",num[i]);
                   {
                       else                   CAN YOU TELL ME WHY THIS ELSE STAMENT IS NOT WORKING and giving me the odd numbers i can get it to do evens
    {
                           printf("\n Odd Numbers are:\n")
                       }
                   //  printf("\n Odd Numbers are:\n");
                   }    
          //    else  
               // this else statement is not working what am i missing for it to print the  
             //  printf("%d\n",num[i]);        
                   }    
                       
               }
           //    printf("\n Odd Numbers are:\n");
               //        for (i=0; i< num; i_++)    
                   {
                       //if (num[i]% 2 !=0)
                       {
                       //    printf("%d\n",num[i]);
                       }
                         
                
       }
    }

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by ElJefe05 View Post
    Hey whiteflags do you have a personal email address. If you could answer some simply questions from me.
    You're basically asking if he's a total loser. That's incredibly rude.

    And why should anyone help you since you don't take the advice anyway? You were told not to use fflush on stdin and you basically told the person to f off and you are still using it.

    And you don't give your posts a proper title ("help!!!" is not a useful title).
    And you don't bother to properly space your code.
    And you post it with a bunch of commented out crap.

    You're lucky that anyone bothers to help you at all.

  11. #11
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    Well thanks for being an ass algorism and i've been using fflush to clear the buffer. Thats what they teached in class so . It's an intro to c++ have a junk basic platform program i'm using. I guess i could of taken the comments out but no reason to be a total db. Forums are meant to help people learn not to bash but thank you .

  12. #12
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    "i've been using fflush to clear the buffer. Thats what they teached in class so."

    Unfortunately, there are many so-called instructors teaching C very badly.

    The use of "fflush(stdin)" is wrong. fflush() is for output buffers only. The code that @whiteflags & the link that @salem gave you shows you is the correct way of flushing any input buffer, stdin, stdin redirected from a file, or a disk file opened for input.
    Code:
    while ((c = getchar()) != '\n' && c != EOF)

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > It's an intro to c++ have a junk basic platform program i'm using
    So you're being taught C++, yet writing C - by a tutor who knows neither.

    It'll be something at least if you realise that the sun doesn't shine out of their posterior, and you treat whatever they say with a degree of scepticism.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your main-declaration isn't complete.
    main should return a integer and if you don't need the parameter, than write 'void' in the function parentheses.
    It should look like:
    Code:
    int main(void) {
    …
    Your problem is a wrong bracket. The bracket in front of the (not working) 'else' is an open-bracket.
    This should be an closing-bracket (i think).
    If you use a editor with parentheses highlighting, you have seen this problem.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Tags for this Thread