How to Sort a Sentence into ASCII Values Without Dynamic Memory?

0
17
Asked By CuriousPenguin93 On

I'm working on a project where I need to sort a given sentence based on ASCII values. However, my professor has laid down some strict rules: I can't use dynamic memory allocation, and the array size can't be predefined from the start. I thought about using variable-length arrays, but the issue is that I end up needing to input the sentence twice—once to gauge its size and again for the actual input. I'm currently using getchar and putchar for input and employing bubble sort to handle the sorting part. I've tried seeking help from AI, but it's been of no use. I'm really stuck and would appreciate any hints or advice you could share!

3 Answers

Answered By CoderSkunk76 On

Honestly, it sounds like your professor wants you to implement a sort where you don’t rely on fixed sizes or allocators. Have you thought about using linked structures, or maybe reading character by character until a delimiter is hit, such as a newline? It would be a bit more complex but could help you bypass the double input issue since you wouldn't have to declare an array at all.

Answered By CodeWizard77 On

You might want to visualize the problem to better tackle it. Grab a whiteboard and sketch out how your inputs would change with sorting. Once you maybe rearrange them visually, it could clarify your approach and how you plan to compare and convert them using your bubble sort.

Answered By TechieGiraffe42 On

Since you can't use dynamic memory, you might consider setting a maximum character limit. A good upper bound could be around 10,000 characters—more than enough for most sentences. You should enforce this limit in your program and throw an error message if it gets exceeded. For the input issue, it’s common in exercises to first collect the size of the input, and then get the actual content. Just make sure you handle errors if the input size doesn't match the expected count, which could simplify your process a bit.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.