Thread: Trial and Error

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    3

    Trial and Error

    New to C and have been given a small task to complete the question is as follows:

    Write a C program that uses an array to store the following values and a loop to display any value which is greater than 50.


    insert
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
    /* array type is integer */
    int value[] = { 20, 40.2, 22, 42.5, 36, 55.78, 39.4, 61, 62, 73.11 };
    int counter, total;
    for ( counter = 50, total = 50; counter > 50; counter++ )
    {
    /* counter variable is acting as the array index */
    /* add the value of the current array element */
    total += value[ counter ];
    }
    printf( "%d\n", total == counter );
    }


    This is the soursecode i have managed so far but it only displays an interger of 1,are there any suggestions as to where i may be going wrong? Please help as i am starting to see doubel

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are printing the value of "total==counter" (which is apparently true, as true will display as 1).

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    3
    thanks tab does that mean i should declare it greater than total or counter? I have tried either way and still displays as either 0 or 1. Thought i might be going wrong elsewhere

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    3
    Still not working when compiled,any other suggestions would be great

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to print the value, then print the value itself, not "total==counter" or "total>counter" or whatever.

    Also: 1) you probably want to print values of the array, not values of the counter; 2) you need to compare the values in the array to 50, not a total of them; 3) if you need to go through the loop and print, then the print statement needs to occur inside the loop; 4) your for-loop, as it stands now, never happens.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Use C's ternary operator "?:" aka conditional expression to print only those values that are > 50.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    It seems like your question
    Write a C program that uses an array to store the following values and a loop to display any value which is greater than 50.
    and your code are two different things. Are you trying to just print all values over 50? You don't need a total for that. Your for loop never executes (50 is not > 50). Just go through the array and if array[index] > 50 print else go to next element.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    60
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
    /* array type is integer */
    int value[] = { 20, 40.2, 22, 42.5, 36, 55.78, 39.4, 61, 62, 73.11 };
    int counter, total;
    for ( counter = 50, total = 50; counter > 50; counter++ )
    {
    /* counter variable is acting as the array index */
    /* add the value of the current array element */
    total += value[ counter ];
    }
    printf( "%d\n", total == counter );
    }
    red: check those value
    indigo: un-neccessary part

    hope this helps !

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Still don't get what you're trying to do but there are a few things that jump out.
    - the for loop block has no terminal condition ie counter > 50?
    - counter >= 50 can't be used as an index for value[] w/o going out-of-bounds.

  10. #10
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Also it looks like value[] should be an array of float or double, not int.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You actually can't usually write a correct program using trial and error. You have to know what you're doing.
    Things to study first:
    if-statements
    for-loops
    printf

    Some more stuff not yet mentioned:
    A number with a decimal point in it is not an integer. Your array is actually holding this: 20, 40, 22, 42, 36, 55, 39, 61, 62, 73

    If you want to know if a value is greater than 50 then you can do this with an if-statement. Do not even consider the ternary operator as itCbitC mentioned.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fatal error LNK1104: cannot open file 'mfc42d.lib'
    By dkennington in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2006, 02:40 PM
  2. flushing stdin
    By crash88 in forum Linux Programming
    Replies: 1
    Last Post: 05-18-2006, 01:21 AM
  3. memory error
    By john_murphy69 in forum C Programming
    Replies: 9
    Last Post: 03-02-2003, 07:28 PM