使用函数填充数组中的数字.如果数组已满,则该数字将在第二个数组中填充
use function to filled an array in number. if the array is full, then the number will be filled on the second array
我在一个名为registered的数组中存储已注册的学生id。如果数组已满,它会自动将学生id存储在另一个名为waitlist的数组中。这个操作将通过一个函数来完成。我不能使我的计算机存储到候补名单阵列的学生id当我注册的数组是满的。我只能使用特定的函数来存储学生id到两个数组。我还需要通过使用相同的功能打印已注册的学生id列表和候补名单。我不知道该怎么做,我找不到一个好的教程和书甚至没有帮助。谢谢你。
这是我的代码。
#include <iostream>
using namespace std;
const int maxCap = 10;
void showOption();
void addToRear(int arr[], int& howMany, int value);
void printArray(int arr[], int howMany);
bool isFull(int capacity, int howMany);
bool isEmpty(int howMany);
int main()
{
bool repeat = false;
bool repeatOption = false;
int reg[maxCap];
int waitlist[maxCap];
int option, value, capacity;
int howMany = 0;
int registered = 0;
int waitlisted = 0;
do
{
showOption();
cout << "please enter your option: 1 ~ 5 " << endl;
cin >> option;
switch (option)
{
case 1:
if(isEmpty(capacity, howMany) == true)
addToRear(reg, howMany, maxCap);
if(isFull(capacity, howMany) == true)
addToRear(waitlist, howMany, maxCap);
break;
case 3:
printArray(reg, howMany);
break;
case 4:
printArray(waitlist, howMany);
break;
case 5:
cout << "thank you for using this program. " << endl;
return 0;
break;
default:
cout << "thank you for using this program. " << endl;
break;
}
//cout << "there are " << howMany << " registered student in this class. " << endl;
cout << "back to main menu? enter 1 for yes: " << endl;
cin >> repeatOption;
}while(repeatOption);
}
void showOption()
{
cout << "Main Menu: " << endl;
cout << "1) Register Student. " << endl;
cout << "2) Unregister Student. " << endl;
cout << "3) Print list of registered student. " << endl;
cout << "4) Print list of waitlisted student. " << endl;
cout << "5) Exit. " << endl;
cout << " " << endl;
}
//void getID(int& value)
//enter student id and store it in array register and waitlist
void addToRear(int arr[], int& howMany, int value)
{
cout << "there are only " << maxCap << " spot for student to be registered." << endl;
cout << "if there are " << maxCap << " students registered in this class, " << endl;
cout << "then the rest of the student will be put on waitlist. " << endl;
cout << " " << endl;
cout << "enter student ID. " << endl;
cout << "enter negative value to stop. " << endl;
int id;
cin >> id;
while((id >= 0) && (howMany < maxCap))
{
arr[howMany] = id;
howMany++;
cin >> id;
}
}
//print list of student that is registered or waitlisted
void printArray(int arr[], int howMany)
{
cout << "there are " << howMany << " number of student in registered list. " << endl;
cout << "the student ID number is " << endl;
for (int index = 0; index < howMany; index++)
{
cout << arr[index];
cout << " " << endl;
}
}
bool isFull(int capacity, int howMany)
{
/if((capacity < 1) || (howMany > 9))
return true;
else
return false;
}
bool isEmpty(int capacity, int howMany)
{
if((capacity > 9) || (howMany < 1))
return true;
else
return false;
}
- 拥有一个同时包含已注册和等待列表的候选人的数组会更容易。
- 添加时,您只需要有一个结束索引。因此,只要指数小于你的截止数字,你的候选人就注册了。
- 删除时,必须在删除的一个候选条目后将列表向上移动一个空格。这样,注册名单将自动更新,以减少候补名单候选人1。
- 打印你必须检查索引,如果它大于截止,你已经注册和等待名单。
您的代码无法识别注册列表和等待列表的满和空条件。我在下面的代码中做了一些修改您可以尝试下面的代码片段:
case 1:
int len = sizeof(reg)/sizeof(reg[0]); // Get the length of reg list
int waitlistLen = sizeof(waitlist)/sizeof(waitlist[0]); //get length of wait list
if(maxCap > len) // Check if reg list not full. Add to reg list
addToRear(reg, howMany, maxCap);
else if(maxCap > waitlistLen) // if reg list is full. Check if wait list is full or not
addToRear(waitlist, howMany, maxCap);
else // if both the list are full show user option menu and break.
showOption();
break;
相关文章:
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 数组/c++中的大量数字
- 遍历并行数组以确定C++中的最大数字
- 整数区间(或 int 数组)中每个数字的出现次数
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 获取 2D 数组 c++ 中的所有数字对
- 为什么使用数组元素查找最大数字的程序不起作用?
- 如何从向量或数组中选择最常见的数字?(前五名)C++
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 以十为基数的数字到布尔数组,该数组要求二进制/基数为 2 的数字
- C++ 检查结果数组中有多少次数字
- 如何在不使用 C++ 中的数组或函数的情况下查找 N 位数字的所有排列
- 如何在C++中有效地将数字值重新分配给字符数组
- 查找数组中的最小数字
- 查找数组中重复(重复)数字的索引
- 我必须找到给定数组中所有数字的周期,就像有很多解决方案,但数组的大小是 10^5
- 是否可以将数组数字作为函数来执行?