Re: Newbie questions

From: Blue Ocean (blueoceanz1_at_hotmail.com)
Date: 01/14/04


Date: 14 Jan 2004 14:36:02 -0800

Randy Howard <randy.howard@FOOmegapathdslBAR.net> wrote in message news:<MPG.1a6f5706f9fa297c989b12@news.megapathdsl.net>...
> In article <3349b232.0401141102.3c979c76@posting.google.com>, blueoceanz1
> @hotmail.com says...
> > Is anyone here willing to take a look
> > at a page long piece of code and help me find the error? It compiles
> > correctly, and it's so simple that you wouldn't think there could be a
> > problem, but there is some problem with the scanf() function somewhere.
>
> A problem with a compilable piece of code using a standard function
> shouldn't be OT here. Why not just post the code and see what kind
> of help you get?

I will, thanks. I will also take a look at the links that the others
posted.

#include <stdio.h>
#include <stdlib.h>

void addNewNumber(float *pointer);
float getLowest(float array[], int length);

int main()
{
        int total;
        
        printf("Numbers to sort: ");
        scanf("%d\n", &total);
        
        float array[total];
        
        for(int i = 0; i < total; ++i)
        {
                addNewNumber(&array[i]);
        }
    
        float lowest = getLowest(array, total);
        
        printf("The lowest number is %1.0f.\n", lowest);
        system("PAUSE");
        return 0;
}

void addNewNumber(float *pointer)
{
        float newNumber;
        printf("New number: ");
        scanf("%f\n", &newNumber);
        *pointer = newNumber;
}

float getLowest(float array[], int length) {
        float lowest = array[0];
        for (int i = 0; i < length; ++i)
                if (array[i] < lowest)
                        lowest = array[i];
          return lowest;
}

THe problem is that the program, when run, gets two numbers before
prompting the second time. It appears that the second number gets
added to the array. Additionally, the program always goes one cycle
longer than it should. In other words, if 10 is the first input, then
the program will scanf eleven times. I can see that that might
possibly be a problem with the for loops (though I checked them many
times to see if I had done something wrong). However, the issue of it
getting two values in a row before printing 'New Number: ', I cannot
fathom the source of. Running it over and over in my head, I cannot
see what would cause it to scan twice, given the order of the calls,
before printing a second prompt.

Thank you for any help in advance.

Yours,

James



Relevant Pages

  • Re: Prime Numbers
    ... /*This code finally compiles as a 'c' code ... One more question....if i don't free up the memory allocated at the end ... it would appear that we are best served by an array ... prime numbers can be represented by an unsigned long int. ...
    (comp.lang.c)
  • Re: Array Out Of Boundaries
    ... int i,j; ... This program is compiling fine and the result is -858993460 50 ... I am unable to find why this is printing the above value. ... So you have 5 elements in the array ...
    (comp.lang.c)
  • Re: Array Out Of Boundaries
    ... int i,j; ... This program is compiling fine and the result is -858993460 50 ... I am unable to find why this is printing the above value. ... So you have 5 elements in the array ...
    (comp.lang.c)
  • Specifying global array size with const int
    ... int main ... Using the #define compiles with no errors, ... part of C to refuse to compile above code with a const int specifying ... array size, when the array is declared global? ...
    (comp.lang.c)
  • Re: argv/pointer problems
    ... > - although this does not alter whether or not it compiles. ... > This may be adaft question, but what is it that makes the ... When defining an array, you _always_ need a size for it. ... int main ...
    (comp.lang.cpp)