C++函数打印数组
C++ functions printing an array
>我正在尝试编写一个小程序,我想打印数组,但它不起作用,对于应用程序,我正在使用 case 函数。
构建一个交互式应用程序,在屏幕上显示五个选项:
读取一串数值;
- 显示字符串;
- 使用插入排序对字符串进行排序;
- 使用按选择排序对字符串进行排序;
- 使用排序气泡排序对字符串进行排序;
- 退出应用程序。
这是我当前的代码:
#include <iostream>
using namespace std;
// int n[12] = {1, 2, 3, 4, 5, 6, 15, 18, 57, 30, 20, 7};
int j, k, nr, n[20];
void values(void) {
cout << "n"
<< "input_values ";
int n[20], nr, i;
cout << "number of elements max 20 ";
cin >> nr;
for (i = 0; i < nr; i++) {
cout << "n[" << i << "] = ";
cin >> n[i];
}
}
void afisare(void) {
cout << "n"
<< "display array; ";
for (int i = 0; i < nr; ++i)
cout << n[i] << ",";
}
在这种情况下,您可以使用function pointer
,例如:
void sort1(int*arr, int n);
void sort2(int*arr, int n);
void sort3(int*arr, int n);
void show(int*arr, int n);
//...
//implement body for the functions above
//...
int main()
{
int arr[5]={5,4,6,3,9};
int n=5;
void (*funcPointer[4])(int*,int)={&sort1,&sort2,&sort3,&show};
int choice=999;
//assume you just type from -1 to 3.
//-1 means exit
while(choice!=-1)
{
cin>>choice;
if(choice==-1) //exit
{
return 0;
}
else
{
funcPointer[choice](arr,n);
}
//and do something what you want
}
return 0;
}
查看更多关于function pointer
的信息 https://www.cprogramming.com/tutorial/function-pointers.html
相关文章:
- 动态分配的聊天数组打印缺失的数据和空
- 对象数组打印空白字符串
- 数组打印"random"值时出现问题
- 数组打印出垃圾值
- 数组打印奇怪的字符,例如 ∟ @
- 测量我的随机数数组打印的时间始终显示为 0 秒
- 数组打印出错误的数字
- 从对象数组打印字符串对象
- 您好,如何防止此函数使用质数数组打印 1
- 如何从主内部的数组打印总计
- 如何将数组打印为矢量形式
- 有人能给我一些关于在C++中使用数组打印出丰富、完美和不足的数字的提示吗
- 为什么我的C++数组打印相同的值
- 如何阻止我的数组打印地址
- 将值输入到结构数组 + 打印输出
- C++11 为什么 cout 从布尔数组打印大整数
- 从字符串数组打印其中一个字符串时出现空指针赋值错误
- 将无符号字符数组打印为十六进制
- 缩短的数组打印错误的结果
- C++中的字符串数组:打印额外的字符