Thread: Help with a C problem for class

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Help with a C problem for class

    Hey everyone ... I am looking for help before my first exam in my C class tomorrow. I have a minimal understanding of C and I was hoping someone could help me with this problem because I am stumped atm.

    QUESTION: Write a comple C program that uses a FOR-loop to read in 41 integers. For each of the 41 integers, first multiply the integer by itself; if the result obtained (i.e., the squared value) is greater than 500, the original integer (before it was squared) should be added into a sum that was initialized to zero. After the loop, multiply the sum by itself and then print out the answer as an integer. Assume correct imput.


    What I have so far ...
    Code:
    #include <stdio.h>
    
    
    int main (void) {
    	
    int sum=0;
    int index;
    int imput;
     
    printf("What number would you like to imput?\n");
    
    
    for (index=0;index<41;index++);
    {
    	scanf("%d", &imput);
    	imput=imput*imput;
    
    
    	if(imput>500) {
    	sum+=imput;	
    	}
    	else(imput<=500); {
    	imput=sum;
    	}
    }
    sum=sum*sum;
    
    
    printf("Your total is now %d.\n", sum);
    
    
    return 0;
    system("pause");
    }
    I am specifically not understanding what to do about the math and how i should use the if statement
    I realize I am screwing up hard ...but I am lost as to where to go from here :C
    Last edited by phatmoney10; 09-08-2016 at 04:50 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You are on the right track, but have a few minor mistakes.

    • It's input with an n, not an m. The program will still work, but an auto-grader may have issues with the misspelling.
    • You are overwriting the original input on line 16. You should declare a second variable called input_squared, and use that.
    • You don't use a "condition" in an else statement. In otherwords, no (imput<=500).
    • Conditions should not have a ; so the one on line 22 should be removed.
    • The above two points are somewhat irrelevant since you don't need an else statement. There are no instructions that define what to do if the square is not greater than 500. In other words, lines 22-24 can go away.
    • The return 0; on line 32 means the system("pause"); on line 33 will never be executed. Either rearrange, or better yet, don't put the pause.

    A few extra notes that are more improvements to readability than anything:
    1. Put some space around your operators. for (index = 0; index < 41;... and input_squared = input * input; etc.
    2. Avoid magic numbers, define constants, and use them in your code. This gives meaningful names to otherwise meaningless numbers, and makes it easier to change a value by only changing the definition instead of scanning and replacing it throughout all your code. This will become much more important as your programs become more complex.
    #define NUM_INPUTS 41
    #define SQUARE_LIMIT 500

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    2
    Quote Originally Posted by anduril462 View Post
    You are on the right track, but have a few minor mistakes.

    • It's input with an n, not an m. The program will still work, but an auto-grader may have issues with the misspelling.
    • You are overwriting the original input on line 16. You should declare a second variable called input_squared, and use that.
    • You don't use a "condition" in an else statement. In otherwords, no (imput<=500).
    • Conditions should not have a ; so the one on line 22 should be removed.
    • The above two points are somewhat irrelevant since you don't need an else statement. There are no instructions that define what to do if the square is not greater than 500. In other words, lines 22-24 can go away.
    • The return 0; on line 32 means the system("pause"); on line 33 will never be executed. Either rearrange, or better yet, don't put the pause.

    A few extra notes that are more improvements to readability than anything:
    1. Put some space around your operators. for (index = 0; index < 41;... and input_squared = input * input; etc.
    2. Avoid magic numbers, define constants, and use them in your code. This gives meaningful names to otherwise meaningless numbers, and makes it easier to change a value by only changing the definition instead of scanning and replacing it throughout all your code. This will become much more important as your programs become more complex.
    #define NUM_INPUTS 41
    #define SQUARE_LIMIT 500

    Thanks so much I think I got it now. I have an exam in my intro to c class tomorrow so I have been studying all night.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with class/derived class
    By ammochck21 in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2007, 12:40 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Class problem
    By Lord JoNil in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2006, 05:18 PM
  4. problem with class
    By Mr.Pink in forum C++ Programming
    Replies: 26
    Last Post: 07-10-2005, 10:24 PM
  5. Class problem...
    By webren in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2005, 11:49 AM

Tags for this Thread