• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C Programming project need help ASAP

kimagurealex

Senior member
This is my gf's project from her school. I don't know C, so I need help from all u guys who knows C. Can u help her to correct her error and make the program works? thanks. it is about using counter, find the median, sum etc. Thanks alot again.

#include <stdio.h>
int main()
{
double array[50], temp, median, sum;
int i, j, counter;

printf(&quot;How many numbers you want to enter: &quot😉;
scanf(&quot;%lf\n&quot;, &amp;counter);

printf(&quot;Enter the numbers you want to sort: &quot😉;

for (i=0; i<counter; i=i+1)
scanf(&quot;%lf &quot;, &amp; array);

printf(&quot;\nThe unsorted numbers are &quot😉;

for (i=0; i<counter; i=i+1)
printf(&quot;%lf &quot;, array);

for (i=0; i<counter-1; i=i+1)
{
for (j= counter-1; j>0; j=j-1)
if(array[j]<array[j-1])
{
temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
}
}


printf(&quot;\nThe sorted numbers are &quot😉;

for (i=0; i<counter; i=i+1)
printf(&quot;%lf &quot;, array);

printf(&quot;min value %lf&quot;, array[0]);
printf(&quot;max value %lf&quot;, array[counter-1]);

if (counter%==0)
median = (array[(counter-1)/2]+array[counter/2])/2;
else
median= array[counter/2+1];
printf(&quot; The median is %lf&quot; , median);

for (i=0; i,counter; i=i+1);
sum= sum+array;
printf(&quot;The sum is %lf&quot;, sum);
printf(&quot;The average is %lf&quot;, sum/counter);



getchar();
getchar();
return 0;
}
 
here's the corrected program. I tried to keep it as close to the original as possible. Common problems....

1. you can't use scanf(....) with array of non char values (non strings). That is scanf(&quot;%lf&quot; aray) is illegal, but you can get the values directly to the indices by supplying the their references to the input function e.x. scanf(&quot;%lf&quot;, &amp;aray[index]).

2. remeber to initialize the variables ( sum in your example is not initialized and would produce an irroneous result)

3. it's a good programming practice to anticipate the impossible, that is catch users stupidity/igorance/etc. in your case you allocate 50 cells for your input values, but never check whether the user's input falls in your anticipated range.

4. it's nice to format the output stream in some orderly fashion

here's the code ( i did not check the sort algorithm, but looks ok )



#include <stdio.h>

int main(int argc, char* argv[])
{

double array[50], temp = 0.0, median = 0.0, sum = 0.0;
int j, k, counter;

while(1)
{
printf(&quot;How many numbers you want to enter: &quot😉;
scanf(&quot;%d&quot;, &amp;counter);
if(counter <= 50 &amp;&amp; counter > 0)
break;
}

printf(&quot;Enter the numbers you want to sort: &quot😉;

for (j=0; j<counter; j++)
{
scanf(&quot;%lf&quot;, &amp;array[j]);
}


printf(&quot;\nThe unsorted numbers are &quot😉;

for (j=0; j<counter; j++)
printf(&quot;%.3lf &quot;, array[j]);

for (k=0; k<counter-1; k++)
{
for (j= counter-1; j>0; j=j-1)
if(array[j]<array[j-1])
{
temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
}
}


printf(&quot;\nThe sorted numbers are &quot😉;

for (k=0; k<counter; k++)
printf(&quot;%.3lf &quot;, array[k]);

printf(&quot;\nmin value %.3lf&quot;, array[0]);
printf(&quot;\nmax value %.3lf&quot;, array[counter-1]);

if (counter%2==0)
median = (array[(counter-1)/2]+array[counter/2])/2;
else
median= array[counter/2];
printf(&quot;\nThe median is %.3lf&quot; , median);

for(j=0; j<counter; j++)
sum += array[j];

printf(&quot;\nThe sum is %.3lf&quot;, sum);
printf(&quot;\nThe average is %.3lf&quot;, sum/counter);

getchar();
getchar();

return 0;
}



edit: it seems like [ i ]s are swallawed by the parser, oh well...lemme replace i with k.
 
Back
Top