Question

read in numbers into array , print out, sort - my program reads in and prints...

read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++

#include <iostream>

using namespace std;

void sortarray(int *tarray, int );
void displayarray(int *[], int);
void averageshowarray(int *[], int);


int main()
{
    int Size;
    int count;
    cout << "enter the size: " << endl;
    cin >> Size;

    int *valptr = new int[Size];


    for(count = 0; count < Size; count++)
    {
        cout << "Enter Test scores: " << endl;
        cin >> *(valptr + count);


    }
    for(count = 0; count < Size; count++)
    {
         cout << *(valptr + count) << endl;
    }

    //sort the array in order.
sortarray (valptr, Size);
    // display the test scores and average.
    //displayarray(valptr, Size);

    //averageshowarray(&valptr, Size);


    return 0;
}
void sortarray(int *b, int amount)
{
    int startscan, minindex;
    int *minelem;

    for(startscan = 0; startscan < (amount - 1); startscan++)
    {
        minindex = startscan;
        *minelem = b[startscan];
        for(int index = startscan + 1; index < amount; index++)
        {
            if((b[index]) < *minelem)
            {
               *minelem = b[startscan];
                b[startscan] = *minelem;
            }
        }
    }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Working c++ code:

#include <iostream>
using namespace std;
void sortarray(int *tarray, int );
void displayarray(int *[], int);
void averageshowarray(int *[], int);

void sortarray(int *b, int amount)
{

int startscan, minindex;
int *minelem;
for(startscan = 0; startscan <= (amount - 1); startscan++)
{
minindex = startscan;
minelem = &(b[startscan]);

for(int index = startscan + 1; index <= amount-1; index++)
{
if((b[index]) < *minelem)
{
minindex = index;
minelem = &(b[index]);
}
}
int tmp = b[startscan];
b[startscan] = *minelem;
b[minindex] = tmp;

}
cout << "Sorted Array is " << endl;
for(startscan = 0; startscan <= (amount - 1); startscan++)
{
cout << b[startscan] << endl;
}
}

int main()
{
int S;
int count;
cout << "enter the size: " << endl;
cin >> S;
int *valptr = new int[S];

for(count = 0; count < S; count++)
{
cout << "Enter Test scores: " << endl;
cin >> *(valptr + count);

}
cout << "Input Array is " << endl;

for(count = 0; count < S; count++)
{
cout << *(valptr + count) << endl;
}
//sort the array in order.
sortarray (valptr, S);
// display the test scores and average.
// displayarray(valptr, S);
//averageshowarray(&valptr, Size);


return 0;
}

Sample Output:

enter the size:
5
Enter Test scores:
2
Enter Test scores:
3
Enter Test scores:
1
Enter Test scores:
5
Enter Test scores:
4
Input Array is
2
3
1
5
4
Sorted Array is
1
2
3
4
5

Add a comment
Know the answer?
Add Answer to:
read in numbers into array , print out, sort - my program reads in and prints...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • Am I using the right cin statement to take in values for my array? If I...

    Am I using the right cin statement to take in values for my array? If I print them out it prints "0x61fea0" with the input 1 2 3 4 5 ///////////////////////////////// #include <iostream> using namespace std; const int ARRAY_LENGTH = 5; int main(){ int numbers[ARRAY_LENGTH]; int threshold; //@todo prompt user to enter array values cout << "Please input 5 numbers: " << endl; for(int i = 0; i < ARRAY_LENGTH; i++){ //@todo add cin statement to read in values for...

  • The purpose of this problem is to sort a 2 dimensional array of characters. Specify the...

    The purpose of this problem is to sort a 2 dimensional array of characters. Specify the size of the array and input the array. If the array is longer than specified, output if it is larger or smaller. Utilize the outputs supplied in main(). Example supplied in 2 test cases. Input 3↵ 3↵ 678↵ 567↵ 456↵ Expected Output Read·in·a·2·dimensional·array·of·characters·and·sort·by·Row↵ Input·the·number·of·rows·<=·20↵ Input·the·maximum·number·of·columns·<=20↵ Now·input·the·array.↵ The·Sorted·Array↵ 456↵ 567↵ 678↵ Input 3↵ 5↵ Ted↵ Mary↵ Bobby↵ Expected Output Read·in·a·2·dimensional·array·of·characters·and·sort·by·Row↵ Input·the·number·of·rows·<=·20↵ Input·the·maximum·number·of·columns·<=20↵ Now·input·the·array.↵ The·Sorted·Array↵ Bobby↵...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers...

    (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). The numbers are displayed in the order of their input and separated by exactly one space. (Hint: Read a number and store it to an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers.) Sample Run Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT