Author Topic: Rearranging Numbers in Ascending Order  (Read 1622 times)

0 Members and 7 Guests are viewing this topic.

Offline Drahgon

  • /dev/null
  • *
  • Posts: 14
  • Cookies: -10
    • View Profile
Rearranging Numbers in Ascending Order
« on: December 15, 2015, 01:01:33 am »
So I have a project where I need to organise people's pancake entries into ascending order depending on how many they ate.

However the current code I have to do it at the moment is faulty and outputs the following:



4710496 comes from nowhere from what I can see. The code I am using is as follows:



My reasoning is that the if statement will sort the largest number in the array and then that number will get stored in k. This makes sense as when it goes through it will take the largest number from all 10 and store it in the holder array (k). The values in array k will then get outputted.

Of course I am going wrong somewhere, but I am very new to arrays and the idea of array manipulation using loops so if anyone could help me I would be greatly appreciative.

Many Thanks,
Drahgon

Offline Kurajber

  • Serf
  • *
  • Posts: 43
  • Cookies: 7
  • Don't Drink and Root
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #1 on: December 15, 2015, 02:55:39 am »
You're using iostream for input and stdio.h for output for some reason. In C you should use stdio.h, iostream is a C++ library.

Printing "k" is pointless because k by itself is actually a pointer to the beginning of that array. You want to print elements of it.

My hints are:
1. First, read all the elements in one loop.
2. Rearrange the array so it fits your criteria. You want ascending order so every element should be greater than all of those before it, not some temp element. Not sure what your idea was with that one too.
3. Print the array correctly.

You should really go learn the basics of the language from the beginning, and not ask people to help you with your homework. And if you do, there are plenty of other places you can do so, this one is primarily for learning.
« Last Edit: December 15, 2015, 02:55:50 am by Kurajber »
0000010100100000

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #2 on: December 15, 2015, 07:48:56 am »
You're using iostream for input and stdio.h for output for some reason. In C you should use stdio.h, iostream is a C++ library.

Printing "k" is pointless because k by itself is actually a pointer to the beginning of that array. You want to print elements of it.

My hints are:
1. First, read all the elements in one loop.
2. Rearrange the array so it fits your criteria. You want ascending order so every element should be greater than all of those before it, not some temp element. Not sure what your idea was with that one too.
3. Print the array correctly.

You should really go learn the basics of the language from the beginning, and not ask people to help you with your homework. And if you do, there are plenty of other places you can do so, this one is primarily for learning.

0.o... What hints at the fact he is *using* C if it compiles fine when he includes the <iostream> header? It's obviously C++, not C. In C++ he should be using <iostream> not <stdio.h> for console I/O typically.
« Last Edit: December 15, 2015, 07:50:02 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline Drahgon

  • /dev/null
  • *
  • Posts: 14
  • Cookies: -10
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #3 on: December 15, 2015, 01:23:44 pm »
You're using iostream for input and stdio.h for output for some reason. In C you should use stdio.h, iostream is a C++ library.

Printing "k" is pointless because k by itself is actually a pointer to the beginning of that array. You want to print elements of it.

My hints are:
1. First, read all the elements in one loop.
2. Rearrange the array so it fits your criteria. You want ascending order so every element should be greater than all of those before it, not some temp element. Not sure what your idea was with that one too.
3. Print the array correctly.

You should really go learn the basics of the language from the beginning, and not ask people to help you with your homework. And if you do, there are plenty of other places you can do so, this one is primarily for learning.

Well the basics of the language are what exactly? I am using variables, loops, conditional statements etc .. the only thing i'm not doing is creating code that efficiently serves this purpose. This is not 'homework', its a starter project I have been working on for 3 days. I'm sorry to burst your pseudo-intellectual bubble, but i'm not expecting you to solve my problem for me. I would just be appreciative of anything that anybody can tell me about how best to understand the rules for sorting through array elements.

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #4 on: December 15, 2015, 03:07:28 pm »
i don't know c ++ but by reading ur code i think ur missing something .

Code: [Select]
for(i=1;i<10;i++){
cin >> s[i];
if(temp<s[i]){
temp=k[i];}
}

Code: [Select]
if(temp<s[i] ){temp=k[i] ;}
the value of k[i] is always 0 so ur temp value is always 0
« Last Edit: December 15, 2015, 03:08:33 pm by blackeagle »

Offline Kurajber

  • Serf
  • *
  • Posts: 43
  • Cookies: 7
  • Don't Drink and Root
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #5 on: December 15, 2015, 03:37:37 pm »
In C++ he should be using <iostream> not <stdio.h> for console I/O typically.

Isn't that exactly what I wrote?

Well the basics of the language are what exactly? I am using variables, loops, conditional statements etc ..

Based on your code I got the feeling that you don't actually understand what most of it does, that's why my suggestion was to start over from the beginning and try to understand how it actually works.

You should have explained your idea first, maybe that would prove otherwise.

I'm sorry to burst your pseudo-intellectual bubble

Not sure if I should respond to this one, so I'll move on.

I would just be appreciative of anything that anybody can tell me about how best to understand the rules for sorting through array elements.

You can learn about sorting algorithms here:
http://www.cprogramming.com/tutorial/computersciencetheory/sorting1.html
« Last Edit: December 15, 2015, 03:38:21 pm by Kurajber »
0000010100100000

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #6 on: December 16, 2015, 03:20:11 am »
^ Not exactly.

Quote
In C you should use stdio.h, iostream is a C++ library

It seemed like you were implying that he is using C and should use stdio.h, because iostream is a C++ library as far as I interpret this. Either way, there's no rulebook that says what he should do. std::cout and std::cin are slower, albeit the more C++ style approach. I know many people who write C++ code like it is C, just so that they can reap the benefits of std::vector and a few other things when they need it -- a few of the very good features of the language of C++ aside from some of the bloat.
« Last Edit: December 16, 2015, 03:20:46 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline Kurajber

  • Serf
  • *
  • Posts: 43
  • Cookies: 7
  • Don't Drink and Root
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #7 on: December 16, 2015, 11:34:34 am »
^ Not exactly.

It seemed like you were implying that he is using C and should use stdio.h, because iostream is a C++ library as far as I interpret this.


Ah, okay, I understand now. The thing is I actually typed first that he should use stdio.h in C and iostream in C++, but then rephrased the second part to "iostream is a C++ library", because I didn't want to imply that in C++ you have to use iostream.

I hope you understand me now, too.
« Last Edit: December 16, 2015, 11:34:43 am by Kurajber »
0000010100100000

Offline fuicious

  • Serf
  • *
  • Posts: 47
  • Cookies: 0
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #8 on: December 16, 2015, 03:21:58 pm »
It's good to break down the problems you have into simpler ones. Then try to solve one after another. My point is, although this problem is simple enough, you know that you have to sort an array. That's something to start with. Google and learn to sort arrays, implement it in your program, and then continue with whatever is next.

It's best if you could sit down and learn C/C++ from the beginning. You're trying to solve something without knowing the right tools and that's why you're stuck. But however, if you prefer to tackle a problem right away, break it down to specific tasks and learn how to do them separately.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #9 on: December 26, 2015, 12:08:39 am »
It's good to break down the problems you have into simpler ones. Then try to solve one after another. My point is, although this problem is simple enough, you know that you have to sort an array. That's something to start with. Google and learn to sort arrays, implement it in your program, and then continue with whatever is next.

It's best if you could sit down and learn C/C++ from the beginning. You're trying to solve something without knowing the right tools and that's why you're stuck. But however, if you prefer to tackle a problem right away, break it down to specific tasks and learn how to do them separately.

That is called 'divide and conquer' in terms of CS theory. Recursion is an example of this, yet recursion isn't always considered a "good" thing and can lead to very inefficient code if the compiler can't optimize it for things like tail-call optimization among other things. Thus it can be good, but usually only as long as you understand these optimization requirements.
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline sonofWrath

  • NULL
  • Posts: 2
  • Cookies: -1
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #10 on: January 16, 2016, 05:07:02 am »
Am I the only one who doesn't see this for loop going on for 10 times ? it goes from 1 to strictly 10, so 9 times if I'm not mistaken. Also this last printf line seems to print the memory address of the array K, rather than elements of that array, hence the big number. You'll probably wanna use another for loop to print the elements of the K array, once you get it to initialize properly (place the numbers in ascending orders). 2cents.

edit: derped.
« Last Edit: January 16, 2016, 06:54:12 pm by sonofWrath »

Offline matias

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: Rearranging Numbers in Ascending Order
« Reply #11 on: February 17, 2016, 01:41:08 am »
Hi there.

Well, as far as I can see, there are a lot of basic concepts you would have to learn before trying to write a program like that. What I know is from the C languaje, but it is also useful for this case.

First: The 'i' index initialization in the 'for' loop ---> You initilalized 'i = 1', therefore ignoring the first element of 's', which is s[0].

Second: temp value assignation and k vector---> you defined an array of integers called 'k', but you never gave any value to it's elements. You should know that the C preprocessor evaluates statemets in a certain order, according to their "weight" in C syntax. In this case, when you write 'temp = k', the preprocessor evaluates the statement from right to left. In other words, it takes the value of 'k' and assignes it to the variable 'temp'. As you never assigned any value to k before that, you're basically giving junk code to temp.

Third: Printing results ---> The "printf( "%d\n", k );" statement is wrong. I think this may be the cause of that 543054569 or somethig number that showed up. 'k' is only a pointer to the first element of the array, this means that you're actually printing a memory adress. If you want to print the elements of an array, you must define an index, let's say 'n' f.e., and print each element of the array in increasing order, until reaching its dimension.

Code: [Select]
int array[10];
int n;

for( n = 0; n < 10; n++ )
    printf( "%d\n", array[i] );

I personally think the code you're trying to write is out of league for the moment. You should focus on the mentioned details first.
I wrote a version of the program in C. I don't know if you'll understand it right now, but I hope it is useful to you in the future, when you have a better knowledge of the subject in matter.

This was my first reply since I registered in this forum, so I hope I helped you bit.
If you have any questions, please don't mind asking them.

Cheers.
printf("%s\n", "Sometimes I like doing redundant things for no reason");