c++ vertical array how to create the output in vertical list.
i try so many times but it cant work.How exactly do I print it out for when I run it? I can't test it because I don't know how to print it out :/ But I feel like I have it wrong anyway. The sample Output should like this
https://docs.google.com/file/d/0B5-VrB_qbetVX2xRQ2ZIYkxzQWc/edit?usp=sharing
#include <iostream>
#include <iomanip>
using namespace std;
#define SIZE 5
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
{
cout<<"Enter Number Please: ";
ioResult = scanf("%d", pFill);
if (ioResult == 1)
{
pFill++;
readCnt++;
}
} while (ioResult == 1 && readCnt < arySize);
cout<<"\n\nOriginal: ";
for(int i = 0; i < SIZE; i++)
cout<<*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;
cout<<"\n\nAscending:\t Descending ";
for (pPrint = pAry, nmbrPrt = 0;
pPrint <= pLast;
nmbrPrt++, pPrint++)
cout<<"\n"<<*pPrint<<"\n<";
for (pPrint = pLast, nmbrPrt = 0;
pPrint >= pAry;
nmbrPrt--, pPrint--)
{
cout<< *pPrint<<"\v";
}
cout<<"\n\nDone\n\n ";
return;
}
Edit
do
{
cout<<"Enter how many times";
cin>>SIZE;
cout<<"Enter Number Please: ";
ioResult = scanf("%d", pFill);
if (ioResult == 1)
{
pFill++;
readCnt++;
}
} while (ioResult == 1 && readCnt < arySize);
How about this 2
int ary[SIZE];
int sary[SIZE];
EDIT : My answer is in pure C. If you want to do C++, I suggest you to use std::vector
and std::sort
Try the following code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 5
void getData (int*);
void printData (int*, int*);
int compare(const void*,const void*);
int main (void)
{
int array[SIZE];
int sorted_array[SIZE];
getData (array);
memcpy(sorted_array, array, SIZE*sizeof(int));
qsort(sorted_array, SIZE, sizeof(int), &compare);
printData (array, sorted_array);
return 0;
}
void getData (int* pAry)
{
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 < SIZE);
}
int compare(const void* pa,const void* pb)
{
int a = *(int*)pa;
int b = *(int*)pb;
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
void printData (int* pAry, int* pSAry)
{
int i=0;
printf("\n+--------------------------------+\n");
printf("| Original | Ascending|Descending|\n");
printf("+----------+----------+----------+\n");
for (; i<SIZE; i++)
{
printf("|%10d|%10d|%10d|\n",pAry[i],pSAry[i],pSAry[SIZE-1-i]);
}
printf("+--------------------------------+\n");
}
Output with 12,15,14,5,8 :
+--------------------------------+
| Original | Ascending|Descending|
+----------+----------+----------+
| 12| 5| 15|
| 15| 8| 14|
| 14| 12| 12|
| 5| 14| 8|
| 8| 15| 5|
+--------------------------------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.