Thread: Array problem

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Question Array problem

    I am writting a program that you enter intergers and am using a find minimum and max functions however I seem to be having a problem when I mix positive and negative # or use a zero. I think I am jusl overlooking the obvious and help is much appreciated.
    include <stdio.h>

    #define MAX_SIZE 25

    int load(int array[])
    {
    int size = 0;

    while (size < MAX_SIZE && scanf("%d", &array[size]) == 1)
    ++size;

    return size;
    }

    void print(int array[], int size)
    {
    int i;
    for (i = 0; i < size; ++i)
    printf("%5d", i);
    printf("\n");
    }

    int findmax(int array[], int size)
    {
    int max = 0;
    int i;

    for (i = 0; i < size; ++i)
    if (max < array[max])
    max = i;

    return max;
    }

    int findmin(int array[], int size)
    {
    int min = 0;
    int i;

    for (i = 0; i < size; ++i)
    if (min > array[min])
    min = i;

    return min;
    }
    int main()
    {
    int array[MAX_SIZE];
    int size;

    printf(" Title: #7\n");
    printf(" By: \n");

    printf("Enter array: ");
    size = load(array);

    printf("Minimum value: %d\n", array[findmin(array, size)]);
    printf("Maximum value: %d\n", array[findmax(array, size)]);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Step through your findmax function yourself (using maybe [-2, 4, 3] for an array) and you'll see pretty quickly what's gone wrong.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    backwatds

    When I run those # its backwards but if I use 1 3 5 it works

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    backwards

    Code:
    int findmax(int array[], int size)
    {
      int max = 0;
      int i;
    
      for (i = 0; i < size; ++i)
        if (max > array[max])
          max = i;
    
      return max;
    }
    
    int findmin(int array[], int size)
    {
      int min = 0;
      int i;
    
      for (i = 0; i < size; ++i)
        if (min < array[min])
          min = i;
    By switching them max > and min < it works ecept when I enter a zero or mit it all up

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'd try something like this
    Code:
    int findmax(int array[], int size) {
        int max = 0;
        int i;
    
        for (i = 0; i < size; ++i)
            if (array[max] < array[i])
                max = i;
        return max;
    }
    same problem with findmin.
    Kurt

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Good

    Works great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM