I have been trying to get an array to sort out 4 numbers that someone enters in. I have been reading on how to do this but nothing will work...
This program is supposed to sort the numbers in ascending and descending order.
This is what I have so far. I'm trying to get them to ascend right now but I'm failing. -
#include "stdafx.h"
-
-
#define ARY_SIZE 4
-
-
int main(void)
-
{
-
printf ("Welcome please enjoy this program.\n\n");
-
-
int num [ARY_SIZE];
-
int i;
-
int j;
-
int x;
-
-
printf ("Enter 4 numbers please.\n");
-
for (int i = 0; i < ARY_SIZE; i++)
-
scanf("%d", &num [i]);
-
{
-
for (int j = i+1; j < ARY_SIZE; j++)
-
{
-
if (num[i] > num[j])
-
{
-
x = num[i];
-
num[i] = num[j];
-
num[j] = x;
-
}
-
}
-
}
-
-
-
-
-
-
return 0;
-
}
-
To print reverse order, you can either resort, or print from the end first and use -- instead of ++. eg set pPrint = pLast, and then check that pPrint >= pAry.
13 5221 Banfa 9,065
Expert Mod 8TB
Your brace at line 17 is mis-place so that by the time the loop on line 18 starts the loop on line 15 has finished. That means that at line 18 i has the value 4, j is initialised to 5 and the loop at line 18 is never entered.
However even if you swap lines 16 and 17 which is what I think you intended this will not work because in that case the loop at line 18 tries to sort the data in the second half to the array but the data for that part of the array has not been entered yet.
You should not be trying to sort in place in the same loop that is actually acquiring the data. Acquire all the data then sort all the data, 2 steps.
Can you use the Standard Library function qsort or are you required to implement the sort function yourself?
If you have to write your own sort function then you should familiarize yourself with the more common sort algorithms. Has your instructor presented any sort algorithms to the class yet?
I have never heard of qsort before so I don't know if we can use that or not. He doesn't really care as long as we get it to do what the problem asked for. As for the algorithms. I don't see anything on algorithms in the chapter we are on. Right now we are learning about pointer applications and all it talks about is dynamic/static arrays and pointer arithmetic. This is an online class so we pretty much have to learn from the book which doesn't help at all.....
Here is the problem i have to do if it helps any:
Write a program that reads integers from the keyboard and places them in an array. The program then will sort the array into ascending and descending order and print the sorted lists. The program must not change the original array or create any other integer arrays.
Don't know if you're allowed to create another integer array only to store the contents of the results. I would seriously check with your professor if you are allowed to create result integer arrays.
If not, try looking for the smallest integer. Then after you've gone through once, look for the next one, etc..
Aside, I don't understand why these beginning programming assignments have such retarded / unclear restraints. The professors who created these assignments seriously need to learn English if they are going to write the questions in English.
Does that mean you can create say a float or double array? *wink
I agree with jkmyoung -- you need clarification from the instructor. On the face of it these instructions are contradictory: you can't sort the input array without either reordering it or writing the sorted numbers into an output array, both of which are prohibited.
I suppose a legalistic pedant might sort the input array into a linked list or binary tree ... those aren't integer arrays. However, your instructor may not be amused.
The only solution I can come up with that meets the requirements is to pseudo-sort the input array into the print queue using an approach vaguely analogous to straight-insertion. This is just another way of stating jkmyoung's approach using more jargon.
If the integers entered from the keyboard were assigned to a string s then another duplicate string say s1 could be originated with s1=s.s could be loaded into an array and left untouched. s1 could be sorted and printed in ascending order and then sorted and printed in descending order. If this actually worked would it be classified as unacceptable obfuscation ?
@whodgson
We are mainly working with arrays right now so I would not be allowed to use a strings. (not that I even know what a string is.....)
Anyways, I haven't talked to him yet but as long as the program runs and does what its supposed to I doubt he will care what I do as long as I am working with arrays. I have done things that we haven't learned before and he didn't seem to mind cause he gave me a 100 and told me what a good job I did (which is his generic comment he gives everyone if the program runs). So if the last part of the question makes this almost impossiable to do then I think he won't mind. I honestly don't think he looks at the codes we send in unless they don't compile.
P.S. This program is due on Friday so I'm kind of starting to freak out.....
Ok this is what I have gotten to work so far (I did the first code completely wrong)... i want the results to look like this though.
Original: (original order they entered the numbers in)
Ascending: (from smallest to largest)
Descending: (from largest to smallest)
I got the Ascending part but don't know how to get the other 2. -
#include "stdafx.h"
-
-
#define SIZE 4
-
-
-
int* getData (int* pAry, int arySize);
-
void selectSort (int* pAry, int* pLast);
-
void printData (int* pAry, int* pLast);
-
int* smallest (int* pAry, int* pLast);
-
void exchange (int* current, int* smallest);
-
-
int main (void)
-
{
-
int ary[SIZE];
-
int* pLast;
-
-
pLast = getData (ary, SIZE);
-
selectSort (ary, pLast);
-
printData (ary, pLast);
-
-
return 0;
-
}
-
-
-
int* getData (int* pAry, int arySize)
-
{
-
int ioResult;
-
int readCnt = 0;
-
int* pFill = pAry;
-
-
do
-
{
-
printf("Enter Number Please: ");
-
ioResult = scanf("%d", pFill);
-
if (ioResult == 1)
-
{
-
pFill++;
-
readCnt++;
-
}
-
} while (ioResult == 1 && readCnt < arySize);
-
-
-
-
-
-
return (--pFill);
-
}
-
-
void selectSort (int* pAry, int* pLast)
-
{
-
int* pWalker;
-
int* pSmallest;
-
-
for (pWalker = pAry; pWalker < pLast; pWalker++)
-
{
-
pSmallest = smallest (pWalker, pLast);
-
exchange (pWalker, pSmallest);
-
}
-
-
return;
-
}
-
-
-
int* smallest (int* pAry, int* pLast)
-
{
-
int* pLooker;
-
int* pSmallest;
-
-
for (pSmallest = pAry, pLooker = pAry + 1;
-
pLooker <= pLast;
-
pLooker++)
-
if (*pLooker < *pSmallest)
-
pSmallest = pLooker;
-
-
return pSmallest;
-
}
-
-
-
void exchange (int* current, int* smallest)
-
{
-
int temp;
-
-
//printf("This is the current value %d and this is the smallest value %d", *current, *smallest);
-
-
temp = *smallest;
-
*smallest = *current;
-
*current = temp;
-
-
return;
-
}
-
-
-
void printData (int* pAry, int* pLast)
-
{
-
int nmbrPrt;
-
int* pPrint;
-
-
-
-
printf("\n\nAscending: ");
-
for (pPrint = pAry, nmbrPrt = 0;
-
pPrint <= pLast;
-
nmbrPrt++, pPrint++)
-
printf("%4d", *pPrint);
-
printf("\n\nDone\n\n ");
-
-
return;
-
}
-
To print reverse order, you can either resort, or print from the end first and use -- instead of ++. eg set pPrint = pLast, and then check that pPrint >= pAry.
THANK YOU SOOOO MUCH!!! now I just need it to print the numbers they originally put in...
here is an example of what I get now that I can get id to print ascending and descending:
lets say the numbers I put in are in this order: 4 7 12 1
This is what i get
Original: 4 4 4 4
Ascending: 1 4 7 12
Descending: 12 7 4 1
See what I mean? I'm stuck on the original part now...
(the code is still the same as the last post I made only difference is I put in an extra printf statement at the very end and on line 43-46 i have this - printf("\n\nOriginal: ");
-
for(int i = 0; i < SIZE; i++)
-
printf("%4d ", *pAry);
..You really can't print the original anymore because you've changed it.
Print the original before you change everything.
Eg make some function printOriginal, and put the first for loop from printData in it.
Then in your main function you have: - pLast = getData (ary, SIZE);
-
printOriginal(ary, pLast);
-
selectSort (ary, pLast);
-
printData (ary, pLast);
So even if you put the printf state up further in your code before it all changes (like where I put it) it still wont orint the original?
Look at lines 43-45 -
-
#include "stdafx.h"
-
-
#define SIZE 4
-
-
-
int* getData (int* pAry, int arySize);
-
void selectSort (int* pAry, int* pLast);
-
void printData (int* pAry, int* pLast);
-
int* smallest (int* pAry, int* pLast);
-
void exchange (int* current, int* smallest);
-
-
int main (void)
-
{
-
int ary[SIZE];
-
int* pLast;
-
-
pLast = getData (ary, SIZE);
-
selectSort (ary, pLast);
-
printData (ary, pLast);
-
-
return 0;
-
}
-
-
-
int* getData (int* pAry, int arySize)
-
{
-
int ioResult;
-
int readCnt = 0;
-
int* pFill = pAry;
-
-
do
-
{
-
printf("Enter Number Please: ");
-
ioResult = scanf("%d", pFill);
-
if (ioResult == 1)
-
{
-
pFill++;
-
readCnt++;
-
}
-
} while (ioResult == 1 && readCnt < arySize);
-
-
printf("\n\nOriginal: ");
-
for(int i = 0; i < SIZE; i++)
-
printf("%4d ", *pAry);
-
-
-
-
return (--pFill);
-
}
-
-
void selectSort (int* pAry, int* pLast)
-
{
-
int* pWalker;
-
int* pSmallest;
-
-
for (pWalker = pAry; pWalker < pLast; pWalker++)
-
{
-
pSmallest = smallest (pWalker, pLast);
-
exchange (pWalker, pSmallest);
-
}
-
-
return;
-
}
-
-
-
int* smallest (int* pAry, int* pLast)
-
{
-
int* pLooker;
-
int* pSmallest;
-
-
for (pSmallest = pAry, pLooker = pAry + 1;
-
pLooker <= pLast;
-
pLooker++)
-
if (*pLooker < *pSmallest)
-
pSmallest = pLooker;
-
-
return pSmallest;
-
}
-
-
-
void exchange (int* current, int* smallest)
-
{
-
int temp;
-
-
//printf("This is the current value %d and this is the smallest value %d", *current, *smallest);
-
-
temp = *smallest;
-
*smallest = *current;
-
*current = temp;
-
-
return;
-
}
-
-
-
void printData (int* pAry, int* pLast)
-
{
-
int nmbrPrt;
-
int* pPrint;
-
-
-
-
printf("\n\nAscending: ");
-
for (pPrint = pAry, nmbrPrt = 0;
-
pPrint <= pLast;
-
nmbrPrt++, pPrint++)
-
printf("%4d", *pPrint);
-
-
printf("\n\nDescending: ");
-
for (pPrint = pLast, nmbrPrt = 0;
-
pPrint >= pAry;
-
nmbrPrt--, pPrint--)
-
printf("%4d", *pPrint);
-
-
printf("\n\nDone\n\n ");
-
-
return;
-
}
-
....... I am so freaking proud of myself right now!!!
I got it to print the original and all I did was change line 45 from:
To: Sign in to post your reply or Sign up for a free account.
Similar topics
by: lawrence |
last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?
Also, when I use sort() on a two dimensional array, it seems to work a
lot like...
|
by: its me |
last post by:
Let's say I have a class of people...
Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class
And I declare an array of this class...
|
by: Federico G. Babelis |
last post by:
Hi All:
I have this line of code, but the syntax check in VB.NET 2003 and also in
VB.NET 2005 Beta 2 shows as unknown:
Dim local4 As Byte
Fixed(local4 = AddressOf dest(offset))
...
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort...
|
by: ritchie |
last post by:
Hi all!
Still working on this program!
Just to recap, I am writing a program to sort an array with four
different sort algorythms.
I am having a little trouble at the moment though!
Now, I...
|
by: ritchie |
last post by:
Hi,
I am writing to ask if anyone can see why my array is not being sorted
correctly?
It's an array of 4 elements(ints 1,2,3,4) but after calling the
selection sort
it comes back sorted as...
|
by: CodeRazor |
last post by:
My array is
int list = new int{20,99,6};
Using a for-loop, how can I order the output of this array. I don't want to
use a sorted list.
I know this is straightforward but can't figure it...
|
by: yeti349 |
last post by:
Hi, I'm using the following code to retrieve data from an xml file and
populate a javascript array. The data is then displayed in html table
form. I would like to then be able to sort by each...
|
by: Jan Smith |
last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a
clear answer to my question. Maybe it's not in Array.Sort.
Here's the question:
I initialize an array X with the...
|
by: Michael |
last post by:
Hi,
I am trying to pass a function an array of strings, but I am having trouble
getting the indexing to index the strings rather than the individual
characters of one of the strings.
I have...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |