Deutsch   English   Français   Italiano  
<vrfdaa$1l75r$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Richard Heathfield <rjh@cpax.org.uk>
Newsgroups: comp.lang.c
Subject: Re: Suggested method for returning a string from a C program?
Date: Wed, 19 Mar 2025 21:35:06 +0000
Organization: Fix this later
Lines: 56
Message-ID: <vrfdaa$1l75r$2@dont-email.me>
References: <vrd77d$3nvtf$2@dont-email.me>
 <slrnvtkrqc.ii1.ike@faeroes.freeshell.org>
 <875xk5v1td.fsf@nosuchdomain.example.com> <vrfadv$1jska$2@dont-email.me>
 <vrfcgn$1l75r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Mar 2025 22:35:06 +0100 (CET)
Injection-Info: dont-email.me; posting-host="b7503893adb236434c108755aac52888";
	logging-data="1744059"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18CSJRQa1iy6X5aRM8ZLM3vKPl+9obzsYVg0ZcyLd2GrQ=="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:4sNhn/HriZ8/LF73zqqu9kluIQs=
In-Reply-To: <vrfcgn$1l75r$1@dont-email.me>
Content-Language: en-GB
Bytes: 2980

On 19/03/2025 21:21, Richard Heathfield wrote:
> On 19/03/2025 20:45, DFS wrote:
>> On 3/19/2025 4:53 AM, Keith Thompson wrote:
> 
> <snip>
> 
>>> I'm actually using
>>> scanf() for integer input, something I wouldn't do in normal code
>>> (because the behavior is undefined if the scanned value is out
>>> of range).  This is a rare case where it's safe to assume that
>>> stdin has nothing harmful.
>>
>>
>>
>> My very 1st submission for 'Missing Number' was accepted, but 
>> each use of scanf() generated a warning:
> 
> Like Keith, I don't usually use scanf.
> 
> My preference is to read the number into a text buffer using 
> fgets, and then convert it using strtoul if I can, or strtol if I 
> have to allow for negatives.
> 
>> //identify the missing number in a set of consecutive integers
> 
> Not sure why you're sorting and malloc-ing.
> 
> Loop through the input, adding as you go.
> Let the first number be x.
> Let the last number be y.
> Let the total of all numbers be t.
> Let m = ((y*(y+1))-(x*(x+1)))/2 - t;
> 
> "The missing number is: %lld\n", m

It occurs to me now that maybe you're sorting in case the numbers 
aren't as consecutive as they're supposed to be, in which case it 
can still be done without sorting, but it's very, /very/ slightly 
harder because you have to keep track of the lowest and highest 
seen so far:

Loop through the input, adding as you go and remembering your two 
extreme values.
Let the lowest number be x.
Let the highest number be y.
Let the total of all numbers be t.
Let m = ((y*(y+1))-(x*(x+1)))/2 - t;

"The missing number is: %lld\n", m

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within