Thread: a program on arrays. need some help.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    a program on arrays. need some help.

    #include <stdio.h>
    main()
    {
    int i;
    int x[16] = { 80, 75, 72, 65, 95, 79, 88, 82, 70, 75, 88, 95, 98, 56, 81, 87};
    float sum = 0, count = 0;
    for (i = 0; i < 16; ++i){
    if (x[i]%2 == 1)
    x[i] = 0;
    sum += (float)x[i];
    }
    for (i = 0; i < 16; ++i) {
    if (x[i] > 0){
    printf("x[%i] = %i\n", i, x[i]);
    ++count;
    }
    }
    printf("what = %.1f\n", sum/count);
    }
    the output being:

    i = 0, x[0] = 80
    sum = 0 + 80 = 80
    i = 1, x[1] = 75
    75%2 = 1
    x[1] = 0
    sum = 80 + 0 = 80
    i = 2, x[2] = 72
    sum = 80 + 72 = 152
    i = 3, x[3] = 65
    x[3] = 0
    sum = 152 + 0 = 152
    i = 4, x[4] = 95
    x[4] = 0
    sum = 152 + 0 = 152
    i'm just trying hard to make sense of this program here.
    so this
    for (i = 0; i < 16; ++i){
    if (x[i]%2 == 1)
    x[i] = 0;
    is going to loop each array and check if the remainder is 1. if so then the array = 0?
    and then that would be added to the number in that array? (0, or that number would be added to the next number if not 0?)
    sum += (float)x[i];
    and is this just checking if the number is positive?
    for (i = 0; i < 16; ++i) {
    if (x[i] > 0){
    printf("x[%i] = %i\n", i, x[i]);
    ++count;
    }
    sorry, quite new to arrays so i'm trying hard to make sense of this. some explanation or guidance would be most appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    PLEASE put code in CODE tags, not QUOTE tags. Quote tags force the code, all the way to the left side of the page, making it a pita to study.

    What are you trying to do here? If you just want to "understand" the program, then just run it, with a bunch of different input values, and also watch the values as you step through the code with a debugger.

    That last bit of code is counting each positive number, and showing their index number and value, to you.

    And Welcome to the forum, Ashlee!
    Last edited by Adak; 10-14-2010 at 12:00 PM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    28
    x[i] = 0; thats what is confusing me.. why exactly is that there? x[i] = 0; i is 0 there for setting x[0] to 0?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    x[i] = 0 happens only when x[i]%2 == 1 for i between 0 and 15. Think about what that means...
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you look at the later part of the program:
    Code:
    if (x[i] > 0){
    So, the idea here is to set the current element to 0 so that it will not be printed later.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Your program is essentially adding up even integers. It's taking the odd integers and setting them to zero. Then it prints out all the array indices and the values of the numbers greater than zero in that array, meaning you will only find even integers in there. Then finally you print out the average of all the numbers that were greater than zero.

    I deduced that from your if(x[i]%2 == 1) { x[i] = 0; } stuff. Clearly x mod 2 is 1 only if x is odd.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    28
    okay so it's starting to make sense here. the index is being added by one like a normal array is.. so if the numbers were 4,8,10,11,13 it would add the totals of each even number and then each odd number would just be +0? so 4+0 = 4 then the second array would be 12 ..22.. 22...22?

    and you said that this last print out gives the average? printf("what = %.1f\n", sum/count);

    how exactly does that work?

    and this

    sum += (float)x[i]; just converts whats in the array to float? what exactly is the point of sum? is that just zero the whole time? and count?

    this is what my compiler gives me when i run this. sorta different
    http://img713.imageshack.us/img713/2384/42086782.gif

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    printf will substitute an expression for the format specifier in the " " part of the printf line of code.

    In this case, it will substitute sum/count for the %f. Obviously, sum is not staying zero the whole time, since 0/any number is nonsense.

    To find an average, you need two things: 1) The sum of the quantities you're averaging, and 2) The number of items that you are putting into the sum.

    If you play with that code a bit, you'll sort it out.
    Last edited by Adak; 10-15-2010 at 11:24 PM.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    28
    sooo we're getting the sum here?
    sum += (float)x[i]

    and what exactly is %.1f?
    Last edited by ashlee; 10-16-2010 at 08:53 AM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    %=a substitution token
    . = a decimal point
    1 = format specifier indicating that 1 digit after the decimal only, should be printed
    f = data type identifier for float.

    Note: a digit before the decimal point indicates the desired width of the field that the number will be printed into.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    28
    i knew that!

    thanks. we're getting there

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by ashlee View Post
    okay so it's starting to make sense here. the index is being added by one like a normal array is.. so if the numbers were 4,8,10,11,13 it would add the totals of each even number and then each odd number would just be +0? so 4+0 = 4 then the second array would be 12 ..22.. 22...22?

    and you said that this last print out gives the average? printf("what = %.1f\n", sum/count);

    how exactly does that work?

    and this

    sum += (float)x[i]; just converts whats in the array to float? what exactly is the point of sum? is that just zero the whole time? and count?

    this is what my compiler gives me when i run this. sorta different
    http://img713.imageshack.us/img713/2384/42086782.gif
    I will get the same thing when I run it. We're all a bit confused here because this program does a little bit more than just computing the average of some even numbers. It also destroys the contents of all the odd integers. Is that what you wanted? By slightly altering your program, I can print out all the integers. Is this what you wanted?

    Code:
    x[0] = 80
    x[1] = 0
    x[2] = 72
    x[3] = 0
    x[4] = 0
    x[5] = 0
    x[6] = 88
    x[7] = 82
    x[8] = 70
    x[9] = 0
    x[10] = 88
    x[11] = 0
    x[12] = 98
    x[13] = 56
    x[14] = 0
    x[15] = 0
    what = 79.2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Replies: 0
    Last Post: 10-29-2001, 11:40 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM