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

  • Thread starter The Sand Man
  • Start date
In summary, DaveE enters data into an array, but when he tries to print the values in the order they were entered, he gets an error because the program deletes values by value instead of by index.
  • #1
The Sand Man
8
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
  • #2
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.
 
  • #3
Yeah, that's what it looks like to me-- you probably meant to have a "break;" after the second n-- statement.

DaveE
 

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

1. What are the common mistakes people make when brushing up on C?

Some common mistakes people make when brushing up on C include forgetting to use semicolons at the end of statements, not declaring variables correctly, and not properly managing memory allocation.

2. Is it necessary to brush up on C if I already know another programming language?

While there may be some similarities between C and other programming languages, brushing up on C is still necessary as it has its own unique syntax and features. Additionally, understanding C can make learning other languages easier in the future.

3. How often should I brush up on C?

It is recommended to brush up on C regularly, especially if you are not actively using it. This can help maintain your skills and prevent forgetting important concepts.

4. Are there any resources or tools that can help with brushing up on C?

Yes, there are many online resources and tools available to help with brushing up on C. These include tutorials, practice exercises, and interactive coding platforms.

5. Can brushing up on C improve my job prospects?

Yes, having a strong understanding of C can make you a more desirable candidate for programming jobs, as it is still widely used in industries such as embedded systems, operating systems, and game development.

Similar threads

Replies
22
Views
3K
Replies
4
Views
887
Replies
25
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
6
Views
2K
Replies
5
Views
2K
Back
Top