Jump to content

Need C program built

SuperRocketMan

Hello, So I'm messing around with the C language and I want to test out some things with arrays. Could someone help write a C program that allows the user to enter up to twenty non-negative integers and store them in an array. Print the numbers (one per line) in the same order they were entered. Then remove all the duplicate numbers from the list and print the numbers with the duplicates removed. Then sort the numbers into ascending order using a bubble sort and print the numbers with the numbers sorted. I also want the user to indicate that all numbers have been entered by typing in a -1. Just trying to get a better understanding of arrays. This program is just for personal use. 

Link to comment
Share on other sites

Link to post
Share on other sites

Why don't you take a crack at it, post your attempt here, and then everyone can try to help make it better. Otherwise you've learned nothing and we did all the work.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

Hello, So I'm messing around with the C language and I want to test out some things with arrays. Could someone help write a C program that allows the user to enter up to twenty non-negative integers and store them in an array. Print the numbers (one per line) in the same order they were entered. Then remove all the duplicate numbers from the list and print the numbers with the duplicates removed. Then sort the numbers into ascending order using a bubble sort and print the numbers with the numbers sorted. I also want the user to indicate that all numbers have been entered by typing in a -1. Just trying to get a better understanding of arrays. This program is just for personal use. 

You will never learn if you don't do your own homework!

Gaming rig- Cpu- Amd 9590, 16gbs of G Skill Ram, Gpu- GTX 760 windforce 3 edition 2gb. A Thermaltake water 2.0 water cooler for my cpu. Keyboard- Thermaltake Posieden , Case- 750D, Mobo Asus 990fx R2.0. 24 inch Dell LED monitor

Link to comment
Share on other sites

Link to post
Share on other sites

The time spent banging your head against the wall is when you learn. Never mind that arrays are at the bottom of the ladder. I suggest you get off the hand holding as soon as possible, will make later concepts easier to swallow.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

The thing is, you only have 2 posts, the 2 posts on this topic, and quite honestly it seems like you just wanted people to make your homework for you. Now if you (as you are doing now, hopefully) gives us some of your work, and explains what you want it to do, and what you think the problem is, you'll see that people are more likely to actually help you out.

Desktop: CPU: i7 3770k OC: 4.0 GHZ | CPUCooler: Corsair H100i | GPU: Asus GTX 670 DirectCU2 | Motherboard: Gigabyte z77x-ud5h | RAM: Corsair Vengeance 16 gig, 1600 mhz | PSU: Corsair AX860 | Soundcard: Asus Xonar Essence STX | Storage: Corsair Force 3 120 gb, Western Digital Caviar Black 2 TB | OS: Windows 8.1 Pro

Periphirals: Keyboard: Logitech g710+ | Mice: Desktop: Razer Imperator Battlefield 3 Edition. Laptop: Razer Deathadder 3.5g edition | Mousepads: Desktop: Razer Goliathus Control Extended edition Laptop: Razer Goliathus Control edition | Sound stuff: Bose Companion 2 speakers (Free yay), Beyerdynamic DT-770 250 Ohm.

Laptop: Lenovo Thinkpad Edge e540: CPU: i7 4702mq | GPU: Nvidia Geforce gt740 | RAM: 8 gig of some brand | Storage: 1 TB of some brand 5400 rpm | OS: Windows 8.1 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

Please refer to this post on how to make these kinds of threads...

 

People using the night theme can see when text is copy-pasted, because that text is actually hard-formatted in black, instead of switching to white in a dark theme. Couple the fact that you just copied this thread with the fact that you made an account simply to get us to do your homework will make people reluctant to help you out.

 

If you try again according to the post mentioned above, maybe we can talk.

Link to comment
Share on other sites

Link to post
Share on other sites

I made this account because I forgot my other account and I deactivated the email associated with it. Here is the code I worked on and could use some help. http://pastebin.com/JYrVcJkV

 

Okay I'm having trouble in my output. If I enter in 20 numbers the program performs and under the "numbers you entered" list the last number is always removed(Unless i type in a -1 before I type in all 20 numbers then it works) why is the last number getting removed under that section. The list with the duplicates removed seems to work. Also I'm very confused on how to create a bubble sort. Anyways sry for the badly worded post. I really just need help. ty

Link to comment
Share on other sites

Link to post
Share on other sites

I made this account because I forgot my other account and I deactivated the email associated with it. Here is the code I worked on and could use some help. http://pastebin.com/JYrVcJkV

 

Okay I'm having trouble in my output. If I enter in 20 numbers the program performs and under the "numbers you entered" list the last number is always removed(Unless i type in a -1 before I type in all 20 numbers then it works) why is the last number getting removed under that section. The list with the duplicates removed seems to work. Also I'm very confused on how to create a bubble sort. Anyways sry for the badly worded post. I really just need help. tyr

Lot better of a post :)

 

Anyways, the issue is the way you are going through your indices (indexes?)

 

Look at this line

for (i=0; i < count; i++)

This is where you are getting your input...I am guessing you actually must have i < 20 or something else like that (as count = 0 was set initially without changing it)...anyways I am assuming that you input for loop runs correctly given your current problem.  So with that running correctly look at this line

for(i=1;i<count;++i)

Now you are going from 1 to (count-1)....this is a problem as count = 20 will only display 19 numbers.  The thing to note is you are using nums[i-1], but if count is lets say 5 you will only display 4 numbers....so really you want i<=count...or the more conventional way

for(i=0;i<count;++i)

and then just use nums

 

I am a bit tired right now so I never really looked over the rest of your code though...if I get time tomorrow I might

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Cool I try some of your tips when I get home I'm at work. I'll update later tonight. If anyone else has any input/suggestions or help pls jump in! 

Link to comment
Share on other sites

Link to post
Share on other sites

So I did what you said and it worked!! tysm but Now I'm confused on how I can sort the numbers into ascending order using a bubble sort

Link to comment
Share on other sites

Link to post
Share on other sites

So I'm having trouble with trying to print the numbers in ascending order using a bubble sort. I just tried to type one up but I don't think I'm right and when I ran it nothing happens under the ascending section. Here's my code:

 

 

printf("\n Your numbers sorted in to ascending order:\n");
 
    for(i=0; i<count; i++)
    {
    for(j=0;j<count;i++)
    {
    if(nums[j]>nums[j+1])
    {
    temp=nums[j];
    nums [j] =nums[j+1];
    nums [j+1] =temp;
    }
 
    }
    }
 
    for(i=0;i<count;i++)
    {
printf("%d\n" , nums[j]);
    }
Link to comment
Share on other sites

Link to post
Share on other sites

Okay, there are a few things wrong with your current sorting algorithm.

 

The one of the most glaring problems I can see is the "j+1" problem.  First j goes from 0 to count, so doing +1 will mean you access count+1 which should not exist (So it would mean j should go from 0 to count-1 instead).

The second of the glaring problems is you used j instead of i while printing out nums....that will be a problem :P

Finally, the beauty of the bubble sort is it requires at slightly less than n2 comparisons (but classified as O(n2)...the way you have it now, it will always do n2 comparisons though.  After you have initially sorted the first element, the last element in the array will be in the correct location.  So instead of going each time to he end of the array you could do one less comparison.  That would make your j for loop look like this (if my logic is right)

for(j=0;j<count - i - 1;i++)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

Finally, the beauty of the bubble sort is it requires at slightly less than n2 comparisons (but classified as O(n2)...the way you have it now, it will always do n2 comparisons though.  After you have initially sorted the first element, the last element in the array will be in the correct location.  So instead of going each time to he end of the array you could do one less comparison.  

 

This is also explained in this video:

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×