Reputation: 2994
Im a newbie to C, I primarily migrated/re-implemeted C programs to Java, etc. I'm trying to learn C and I have gone through several tutorials. I have a program that attempts to read user input(numbers) from command line via char *argv
, sort those numbers and just print the sorted numbers out.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
//if the is argument supplied
if (argc > 1)
{
int count = 0;
//array to sort less 1
count = argc -1;
int tobe_sorted[count];
int i=0;
//allocate memory for target result array
int *target = malloc(count * sizeof(int));
//copy unsorted ints to target
memcpy(target, tobe_sorted, count * sizeof(int));
//sort target using bubble sort
int temp =0;
int x =0;
int y=0;
for (x=0; x<count; x++ )
{
for (y=0; y<count -1; y++ )
{
if (target[i]>target[i+1])
{
temp = target[i+1];
target[i+1] = target[i];
target[i] = temp;
}
}//end inner
}//end outer
//end bubble sort
x=0;
printf("Sorted:\n");
//print sorted array
while (x <count)
{
printf("%d",target[x]);
printf(",");
x++;
}
printf("\n");
}//end argument supplied
else{
printf("Error, please enter numbers to be sorted\n");
}
return 0;
}//end main function
PS C:\Users\xyz\workspace_cpp\the_hard_way\ex18> .\sort_test.exe 5 4 3 2 1
Sorted:
2686748 1986196690 32 1 4199492
Disclaimer: Im not looking to use library functions such as qsort(), or to make the program more efficient.
Edited & working code based on comments (there is an extra index being allocated in target array, otherwise sorting & char to int conversion is working):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
//if thee is argument supplied
if (argc > 1)
{
int count = 0;
count = argc;
int tobe_sorted[count-1];
//initialize with 0's
int i=0;
for (;i<count; i++)
{
tobe_sorted[i] = 0;
}
//populate tobe_sorted with integer values of argv
i=0;
for (;i<count; i++)
{
tobe_sorted[i] = atoi(argv[i]);
}
//allocate memory for target result array
int *target = malloc((count * sizeof(int)));
//copy unsorted ints to target
memcpy(target, tobe_sorted, count * sizeof(int));
//sort target using bubble sort
int tmp =0;
int x =0;
int y=0;
int swapped = 1;
while (swapped)
{
swapped =0;
y++;
for (x=0; x < count - y; x++)
{
if (target[x] > target[x+1])
{
tmp = target[x];
target[x] = target[x+1];
target[x+1] = tmp;
swapped = 1;
}
}
}
//end bubble sort
x=0;
printf("Sorted:\n");
//print sorted array
while (x <count)
{
printf("%d",target[x]);
printf(",");
x++;
}
printf("\n");
}//end argument supplied
else{
printf("Error, please enter numbers to be sorted\n");
}
return 0;
}//end main function
Input:
PS C:\Users\xyz\workspace_cpp\the_hard_way\ex18> .\sort_test.exe 5 -3 -2 1 8
Output Sorted: -3,-2,0,1,5,8,
Upvotes: 0
Views: 277
Reputation: 1926
You have several problems:
int tobe_sorted[count];
This is uninitialized. It currently contains junk. You're then copying that junk to your target array.
//copy unsorted ints to target
memcpy(target, tobe_sorted, count * sizeof(int));
As mentioned in 1, your tobe_sorted
array doesn't contain your data. You want to parse *argv[]
, which is an array of arrays that contains the actual cmdline input. argc
just tells you how many elements exist in *argv[]
array.
You need to parse the argv
array, convert each element into an int
and store the results in the tobe_sorted
array.
Upvotes: 2
Reputation: 133577
The main problem here is that you are not using argv
at all.
You declare an array named tobe_sorted
, you don't initialize its values (so it contains garbage).
Then you copy the data on the target
array and you sort that garbage data. Then you print them.
You must parse argv
, convert each argument to an int
using something like atoi
and then sort these values.
Upvotes: 2
Reputation: 29265
You never initialize target
or tobe_sorted
- they are "full of junk".
You never take the numbers off your command line argv
and do anything with them.
You're going to need to convert the strings in argv
into ints
. Something like this:
for(i = 1; i < count; i++) {
tobe_sorted[i-1] = atoi(argv[i]);
}
Before your memcpy()
Upvotes: 3