Brushing Up On [C], What's Wrong?

  • Thread starter Thread starter The Sand Man
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion centers on a C programming issue where the user attempts to sort an array of input values in ascending order after terminating input with a negative value. The provided code incorrectly deletes multiple instances of the minimum value due to its logic, leading to incorrect output. The solution involves adding a "break;" statement after the second decrement of 'n' to prevent multiple deletions of the same value. This adjustment ensures that only one instance of the minimum value is removed during each iteration.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Knowledge of arrays and loops in C
  • Familiarity with input/output functions in C, specifically scanf and printf
  • Basic understanding of sorting algorithms and their implementation
NEXT STEPS
  • Review C programming array manipulation techniques
  • Learn about sorting algorithms, specifically selection sort and bubble sort
  • Explore debugging techniques in C to identify logical errors
  • Investigate memory management in C, particularly dynamic arrays
USEFUL FOR

Programmers looking to improve their C coding skills, particularly those interested in array manipulation and sorting algorithms. This discussion is also beneficial for individuals troubleshooting logical errors in their code.

The Sand Man
Messages
8
Reaction score
0
I haven't written C in a while, but I decided to brush up on it.

I'm trying to solve this problem:

User inputs values into an array, input terminates with a negative value. I then want to print out all the values in the order they were entered, then print them in ascending order.

I realize there are multiple ways to solve this problem, but this is the only one which is giving me trouble. The approach I have in this problem is to work through the array, find the lowest value, print it, then delete it.

Code:
#include<stdio.h>
#include<math.h>

int main()
{
	double x[100],v, min=0;
	int i, n, j, count;
	printf("Enter data (<0 to terminate): ");
	n=0;
	while(1)
	{
		scanf("%lf",&v);        //Scan in values
		if(v<0.0) break;        //Terminate with negative value
		x[n++]=v;               //Store in array
	}
	printf("given data:\n");
	for(i=0;i<n;i++)
	{
		printf("%5.1f",x[i]);        //Print out array values
	}
	printf("\ndata in ascending order:\n");
	count=n;
	while(count>0)
	{
		min=x[0];
		for(i=0;i<n;i++)
		{
			if(min>x[i]) min=x[i];       //Find lowest value
		}
		count--;
		printf("  %.1lf", min);              //Print lowest value
		i=0;
		while(i<(n-1))                       //Delete lowest value
		{
			if(min==x[n-1])             //Check if it's the last one, reduce length of array
			{
				n--;
				break;
			}
			if(x[i]==min)                //If it's not at the end, delete it, and overwrite array from that position onwards
			{
				for(j=i;j<(n-1);j++)
				{
					x[j]=x[j+1];
					
				}
				n--;
			}
			i++;
		}
	}
	printf("\n");
	return 0;
}

I hope you can figure out my mistake. When I input values like:
1 3 1 5 1 5 1
I get:
1 1 3 5 5 5 5 :(

Thanks for any help. :)

Cheers,
Sand
 
Technology news on Phys.org
Since the program deletes by value instead of by index of the minimum numer it ends up deleteing multiple instances of "1" in the loop, resulting in left over "5"'s.
 
Yeah, that's what it looks like to me-- you probably meant to have a "break;" after the second n-- statement.

DaveE
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
47
Views
5K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K