This is a sort question.

Asked 1 years ago, Updated 1 years ago, 38 views

It is written in c++.I would like to sort sum[10] in descending order and display it. What should I do?Right now, it just shows all the sum, but it doesn't show up in a sorted state.

#include<stdio.h>

int main()
{
    FILE*f;
    float example_result[30], xmax, sum[10], sumdummy;
    inti,j,jmin,n;

    f = fopen("filename", "r");
    if(f==NULL){
        printf("Cannot open filename" nn");
        return1; 
    }

    for(i=0;i<30;i=i+3)
        fscanf(f, "%g", example_result[i]);

    fclose(f);

    if(n==0){
        printf("No data in filename);
        return1;
    }

    for(j=0;j<=10;j++){
        sum[j]=exam_result[3*j]+exam_result[3*j+1]+exam_result[3*j+2];
    }

    for(j=0;j<10;j=j+1){
        if(sum[j]<sum[jmin])jmin=j;
    }

    if(j!=jmin){
        sumdummy=sum[j];
        sum[j] = sum[jmin];
        sum[jmin] = sumdummy;
    }

    for(j=0;j<=3;j++){
        printf("%g", sum[j]);
    }

    return 0;
}

c++

2022-09-30 19:31

2 Answers

"Don't use ""std::sort"" is not very helpful to non-questioners, so I will answer it as a C++ language."Also, there are various sorting algorithms.The questioner must first consider which one to use, or the questionnaire will not be completed.

For sorting in C++ language, std::sort is provided.In particular, std::partial_sort is available for sorting specifically to the top four.Use std::greater to make a comparison in descending order.Addresses are acceptable to represent sorting ranges, but you can also use std::begin and std:end.

To use these to output in a single line without opening up to 4th place in descending order, you can write:

#include<algorism>
# include <fstream>
# include <iostream>
# include <iterator>

int main() {
    US>structure{
        float kokugo, suugaku, eigo;
    } results [10];
    if(std::ifstream f {"filename"};!f){
        std::cout<<"Cannot open filename"<<std::endl;
        return1;
    } else{
        for (auto&[kokugo, suugaku, eigo]—results)
            f>>kokugo>>suugaku>>eigo;
    }

    float sum [10];
    int j = 0;
    for (auto&[kokugo, suugaku, eigo]—results)
        sum [j++] = kokugo+suugaku+eigo;

    std::partial_sort(std::begin(sum), std::begin(sum)+4, std::end(sum), std::greater{});

    for (inti=0; i<4;i++)
        std::cout<<sum[i];
    return 0;
}


2022-09-30 19:31

The purpose is
- I want to sort them out
- Want to understand the sorting algorithm (by implementing )
The answer will be different depending on which one, but which one is it?

If the former one is std::sort.There's no way not to take advantage of it because it's used by programmers all over the world and has finished products that would have already been out if there were some bugs (it's called wheel reinvention and it's a symbol of waste)

In the latter case, there are many types of sorting algorithms, each with its own strengths and weaknesses, making it difficult to describe everything in the answer section here. At least narrow down the question to "Mr. Andai, I want to sort it out."

When you look at other questions, you can't just have a score, you have to keep and sort attendance numbers and scores as a set, or you'll be able to achieve your goal. Then you need to sort class objects, and you'll be happy to answer them.


2022-09-30 19:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.