Re: Newbie questions
From: Blue Ocean (blueoceanz1_at_hotmail.com)
Date: 01/14/04
- Next message: Blue Ocean: "Re: Newbie questions"
- Previous message: Robert Bachmann: "Re: Logical And"
- In reply to: Randy Howard: "Re: Newbie questions"
- Next in thread: nrk: "Re: Newbie questions"
- Reply: nrk: "Re: Newbie questions"
- Reply: Sidney Cadot: "Re: Newbie questions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Blue Ocean: "Re: Newbie questions"
- Previous message: Robert Bachmann: "Re: Logical And"
- In reply to: Randy Howard: "Re: Newbie questions"
- Next in thread: nrk: "Re: Newbie questions"
- Reply: nrk: "Re: Newbie questions"
- Reply: Sidney Cadot: "Re: Newbie questions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|