繁体   English   中英

如何让 arrays 显示升序和降序

[英]How do I get arrays to display ascending and descending

我是stackoverflow的新手,我已经完成了评估,但我似乎无法让它按照我想要的方式工作。 我的第一部分工作正常,但数组的第二部分不工作,而且我希望它以升序和降序显示数组。 任何帮助都会非常感谢

// Test4.cpp : This file contains the 'main' function. Program execution begins and ends there.

//

/* For this assessment I am required to create a menu system consisting of 3 options 1 to generate a username, 2 to sort an array in ascending and descending order and 3 to exit.


The menu should be displayed as shown


1. String Functions

2. Array Functions

3. Exit

*/

#include <iostream>
#include <string>
#include <algorithm>
#include <conio.h>

using namespace std;

// Here are the methods I will use

void display_menu();

void create_name();

void menu_act_on_choice(int choice_in);
void generate_and_display_name(string name_in);

//global double array

double calcTimes[] = { 21.23,23.45,23.71,22.22,24.12,21.23,21.23,21.45 };

void display_menu()
{
  /*
do
display menu options
prompt for and read value for choice
if choice <> 4 (quit option)
call menu_act_on_choice passing value of choice
while choice is not equal to 4 (quit option)
*/
  // local variables
  int choice = 0;

  do
  {
system("cls");
cout << "Make your selection from the following options" << endl
  << "1. String Functions" << endl
  << "2. Array Functions" << endl
  << "3. Exit" << endl;
cout << "> ";
cin >> choice;
menu_act_on_choice(choice);
} // end do

  while (choice != 4);

} // end method

void menu_act_on_choice(int choice_in)
{
  switch (choice_in)
{
  case 1:
  create_name();
  break;

  case 2:
  void array_length();
  break;

  case 3:
  break;
}// end switch

}

void create_name()
{
  /*
prompt for first name and second name
using getline read in name in format first name and last name
ignore white space
call create_name() passing name as parameter
*/
  // local variables
  string name = "";
 
cout << "Enter your full name: ";
cin.ignore();
getline(cin, name);
generate_and_display_name(name);
} // end method

void generate_and_display_name(string name_in)
{
  // local variables
  string string1;
  int position = 0;
  string string2;

  // add first character to string 1
string1.assign(name_in, 0, 1);

  // find position of space
position = name_in.find(" "); // position of first space

  // assign lastname to string 2, position +1 to end of string
string2.assign(name_in, position + 1, name_in.length());

  //concatenate both strings
string1.append(string2);

  // Print name
cout << "Your username is " << string1 << endl;
cout << "Press any key to return to the menu ";
cin.ignore();
} // end method

void array_length()
{
  // stage 1 find the total size of the array in bytes

  int stage1 = sizeof(calcTimes);

cout << "The array is " << stage1 << " bytes" << endl;

 

  // stage 2 find the size of a single element

  int stage2 = sizeof(calcTimes[0]);  // or sizeof(int)

cout << "A single element is " << stage2 << " bytes" << endl;

 

  // stage 3 calculate how many elements stage 1 / stage 2

 

  int stage3 = (stage1 / stage2);

cout << "There are " << stage3 << " elements" << endl;

}

 

int main()

{

display_menu();

}

我建议使用std::sort

例子:

#include <array>       // std::begin, std::end
#include <algorithm>   // std::sort
#include <functional>  // std::less, std::greater

#include <iostream>

int main() {
    double calcTimes[] = { 21.23,23.45,23.71,22.22,24.12,21.23,21.23,21.45 };
    

    // ascending order (std::less<>{} is the default and can be left out)
    std::sort(std::begin(calcTimes), std::end(calcTimes), std::less<>{});

    // print the result
    for(double value : calcTimes) std::cout << value << '\n';


    // decending order
    std::sort(std::begin(calcTimes), std::end(calcTimes), std::greater<>{});

    // print the result
    for(double value : calcTimes) std::cout << value << '\n';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM