Another C question (sorting arrays)

Started by
3 comments, last by szecs 13 years, 4 months ago
Hi again,

Sorry to be asking so many questions, but I have another problem.

I'm trying to write a program that takes command line parameters and sorts them, then does various operations on them. Everything works fine except for the sorting.

#include <stdio.h>#include <stdlib.h>float variance(char* data[], float average, int num);void swap(char *a, char *b);int main(int argc, char *argv[]){	int i;	float total = 0;	float mean = 0.0;	printf("Data: ");	for (i = 1; i < argc - 1; i++)	{		if (argv > argv)<br>		{<br>			swap(&amp;argv<span style="font-weight:bold;">, &amp;argv);<br>		}<br>	}<br><br>	<span class="cpp-keyword">for</span> (i = <span class="cpp-number">1</span>; i &lt; argc; i++)<br>	{<br>		printf(<span class="cpp-literal">"%s  "</span>, argv<span style="font-weight:bold;">);<br>	}<br>	<br>	printf(<span class="cpp-literal">"\nMaximum: %s\n"</span>, argv[argc - <span class="cpp-number">1</span>]);<br>	printf(<span class="cpp-literal">"Minimum: %s\n"</span>, argv[<span class="cpp-number">1</span>]);<br><br>	<span class="cpp-keyword">for</span> (i = <span class="cpp-number">1</span>; i &lt; argc; i++)<br>		total += atoi(argv<span style="font-weight:bold;">);<br><br>	mean = total / (argc - <span class="cpp-number">1</span>);<br><br>	printf(<span class="cpp-literal">"Mean: %.2lf\n"</span>, mean);<br>	printf(<span class="cpp-literal">"Variance: %.2lf\n"</span>, variance(argv, mean, argc));<br>}<br><br><span class="cpp-keyword">float</span> variance(<span class="cpp-keyword">char</span>* data[], <span class="cpp-keyword">float</span> average, <span class="cpp-keyword">int</span> num)<br>{<br>	<span class="cpp-keyword">float</span> sum = <span class="cpp-number">0</span>.<span class="cpp-number">0</span>;<br>	<span class="cpp-keyword">int</span> i;<br><br>	<span class="cpp-keyword">for</span> (i = <span class="cpp-number">1</span>; i &lt; num; i++)<br>	{<br>		sum += (atoi(data<span style="font-weight:bold;">) - average) * (atoi(data<span style="font-weight:bold;">) - average);<br>	}<br><br>	<span class="cpp-keyword">return</span> (sum / (num - <span class="cpp-number">1</span>));<br>}<br><br><span class="cpp-keyword">void</span> swap(<span class="cpp-keyword">char</span> *a, <span class="cpp-keyword">char</span> *b)<br>{<br>	<span class="cpp-keyword">char</span> tmp = *a;<br>	*a = *b;<br>	*b = tmp;<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>I've also tried sorting inside main without using swap and a few other ways, but none of them really do anything.<br><br>What am I doing wrong?<br><br>Thanks
Advertisement
Inside your swap function: try making char tmp a pointer and point to &a instead of just a.
I thought & was the address and * was the value. Either way, I tried it and it didn't work. Thanks for trying to help though.
The main issue you have with the sort is you are attempting to deal with command line parameters as if they are numerical. In actual fact argv is an array of pointers to character strings. I see you are using atoi() in the variance function - you need to extend that to the rest of the code.

What you probably want to do is loop over each argument and call atoi() on it to convert the string to an integer. You could use a std::vector<int> and push_back each integer into it. You can then use the vector as you are using argv. Now you only have to convert the arguments once.

The second problem with your sort is that you are doing a bubble sort but only passing over the array once. You need to pass over the array several times until you do not swap any values. The array will then be sorted.

There is of course lots of error checking you should do on your input data to ensure the user cannot break your program but that can wait!
for( i = 0; i < argc - 1; i++ ){   for( k = i+1; k < argc; k++ )   {      if (argv > argv[k])      {          swap(&argv, &argv[k]);      }   }}

This topic is closed to new replies.

Advertisement