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

  • Thread starter Thread starter The Sand Man
  • Start date Start date
AI Thread Summary
The discussion centers on a C programming challenge involving user input of values into an array, which terminates upon entering a negative number. The goal is to print the values in the order they were entered and then sort them in ascending order. The user shares their code, which attempts to find and delete the minimum value iteratively. However, the output is incorrect, showing multiple instances of the same value instead of unique sorted values. The issue arises because the program deletes by value rather than by index, leading to unintended deletions of duplicate values. A suggestion is made to include a "break;" statement after the deletion of the minimum value to prevent further iterations from affecting the remaining values. This highlights the importance of correctly managing array indices during sorting operations in C.
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top