spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Get the unknown length of number from input


First off, massive kudos for testing the return value from scanf, most people just blindly assume it works. It's just a shame you're using it in the wrong way :-)

You want the loop to continue as long as the return value is 1, meaning that you managed to scan an integer. That means it should be:

while (scanf ("%d", &i) == 1) {

That also means that any non-numeric input will cause scan failure and hence the while loop will exit. So, if you enter:

3 1 4 1 5 9 stop

you should successfully see the numeric values from the array.

The only other thing is to clean up your j handling since the k loop will stop early. This can be done with:

for (k = 0; k <= j; k++) {

Alternatively, leave that loop alone and just change how you initialise and modify j:

int j = 0;
:
arra[j++] = i;

I tend to find the second choice more C-like since j is then a count of the elements in the array rather than the maximum index.


And, of course, you're open to a buffer overflow attack at the moment since you assume nobody will enter more than a hundred numbers. So, don't use this as a homework solution (what you have is good enough with the slight bug fixes) but I'd tend to write it as something like:

#include<stdio.h>

#define SZ 100

int main (void){
  int arra[SZ], i, nxt = 0;

  while ((nxt < SZ) && (scanf ("%d",
&(arra[nxt])) == 1))
    nxt++;
  printf ("
");

  for (i = 0; i < nxt; i++)
    printf ("%d ", arra[i]);

  return 0;
}

Categories : C

Related to : Get the unknown length of number from input
C++ Code to calculate sum of unknown number of numbers
My guess is you never ended your input. It will continue looping and adding numbers until you trigger end of file. Just pressing enter won't do that - you need to hit ctrl+d on Linux or ctrl+z on Windows to end the standard input file, allowing the while loop to exit.

Categories : C++
Creating a range with fixed number of elements (length)
I use numpy for this. >>> import numpy as np >>> np.linspace(start=0, stop=7.5, num=4) array([ 0. , 2.5, 5. , 7.5]) >>> list(_) [0.0, 2.5, 5.0, 7.5]

Categories : Python
XML to XSL mapping - how to check the length of the element code's value - string(length)
I am not sure what you mean by "retrieves both values from the XML simultaneously", but consider using a key to look up the composite elements by their code value. <xsl:key name="comp" match="segment[@code='ACK']/composite[@code='ReceivedQty']" use="element[@code='CODE']/value" /> Then your current xsl:value-of can be simplified to this <xsl:value-of select="key('c

Categories : Xml
How to restrict input number to non-decimal
You can make a custom directive for that, incorporating the ngModelController. Like this: Proof of Concept module.directive("noDecimalInput", function() { return { restrict: "A", require:"ngModel", link: function(scope,element,attr,ctrl) { ctrl.$parsers.push(function(value){ if(value.indexOf(".")>-1) { ctrl.$setVal

Categories : HTML
File.length() in Java returns incorrect length
From Javadoc: Returns: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.` Are you sure the file path is correct?

Categories : Java
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.