Reputation: 147
I am trying to implement bubble sort in C. The question has a constraint that:
The first line should be the number of elements in the array
The second line is the input
For example:
first line: 5
second line: 5 4 2 8 1
I need to read the second line an storing it into an array to be able to sort them. I have searched on the internet and found that getline()
helps us to read a line. The issue here is that if I want to use getline()
I need to know how many bits are going to read so I can allocate enough space for it, but here we do not know how along a number would be.
The code I have written is as follow:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv){
int sizeOfArray;
// getting the number of inputs from the second line.
// Then go to the third line
scanf("[^\n]%d[^\n]", &sizeOfArray);
int inputArray[sizeOfArray];
//------------------------------------
// This bit I have stuck. I do not know how to solve it
for(int iteration = 0; iteration<sizeOfArray; iteration++)
{
("%d ", &inputArray[iteration]);
}
// ----------------------------
int arrSizeCopy = sizeOfArray;
int mediator = 0 ;
while(arrSizeCopy> 0)
{
for(int i=0; i< arrSizeCopy-1; i++)
{
if(inputArray[i]>inputArray[i+1])
{
mediator = inputArray[i];
inputArray[i] = inputArray[i+1];
inputArray[i+1] = mediator;
}
}
arrSizeCopy--;
}
for (int i=0; i<sizeOfArray; i++)
{
printf("%d ", inputArray[i]);
}
return 0;
}
I really appreciate if someone could help me to find the answer for it.
Upvotes: 1
Views: 978
Reputation: 44274
This line
scanf("[^\n]%d[^\n]", &sizeOfArray);
seems a bit strange.
So I changed your code like
if (scanf("[^\n]%d[^\n]", &sizeOfArray) != 1)
{
printf("Bad luck\n");
return;
}
and gave it the input you describe, i.e. 5. The result was Bad luck So the scanf
failed and sizeOfArray
is uninitialized (which is very bad).
Instead simply try
if (scanf("%d", &sizeOfArray) != 1)
{
printf("Bad luck\n");
exit(1);
}
and you'll see it work.
Also change
scanf("%d ", &inputArray[iteration]);
into
if (scanf("%d", &inputArray[iteration]) != 1)
{
printf("Bad luck\n");
exit(1);
}
When using scanf
you shall always check the return value.
A complete example for reading and printing data:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
int sizeOfArray;
if (scanf("%d", &sizeOfArray) != 1)
{
printf("Bad luck\n");
exit(1);
}
int inputArray[sizeOfArray];
for(int iteration = 0; iteration<sizeOfArray; iteration++)
{
if (scanf("%d", &inputArray[iteration]) != 1)
{
printf("Bad luck\n");
exit(1);
}
}
for (int i=0; i<sizeOfArray; i++)
{
printf("%d ", inputArray[i]);
}
return 0;
}
Input:
5
1 2 3 4 5
Output:
1 2 3 4 5
Upvotes: 0
Reputation: 608
this is from the getline man page, and it looks to answer your concerns
"If *lineptr is set to NULL and *n is set 0 before the call, then
getline() will allocate a buffer for storing the line. This buffer
should be freed by the user program even if getline() failed.
Alternatively, before calling getline(), *lineptr can contain a
pointer to a malloc(3)-allocated buffer *n bytes in size. If the
buffer is not large enough to hold the line, getline() resizes it
with realloc(3), updating *lineptr and *n as necessary."
http://man7.org/linux/man-pages/man3/getline.3.html
Upvotes: 2