할당에는 3 개의 기능이 필요합니다. 사용자는 10을 입력 할 때까지 숫자 0-9를 입력하고 입력을 중지하고 각 숫자를 센 다음 각 숫자의 개수를 계산합니다. 사용자가 숫자를 입력 한 경우에만 출력해야합니다.초심자 배열, C++에 대한 문제
내 유일한 문제는 사용자가 사용하지 않는 배열의 모든 요소에 대해 Xcode는이를 0으로 계산하므로 최종 출력의 수가 0이 비정상적으로 많습니다. 다른 모든 것은 잘 작동합니다.
여기에 당신이 "0으로 [보내고] 엑스 코드 수"로 언급하고하는 것은 실제로 단지 초기화되지 않은 값이다
#include <iostream>
using namespace std;
// counter function prototype
void count(int[], int, int []);
// print function prototype
void print(int []);
int main()
{
// define variables and initialize arrays
const int SIZE=100;
int numbers[SIZE], counter[10], input;
// for loop to set all counter elements to 0
for (int assign = 0; assign < 10; assign++)
{
counter[assign]=0;
}
// for loop to collect data
for (int index=0 ; input != 10 ; index++)
{
cout << "Enter a number 0-9, or 10 to terminate: ";
cin >> input;
// while loop to ensure input is 0-10
while (input < 0 || input > 10)
{
cout << "Invalid, please enter 0-9 or 10 to terminate: ";
cin >> input;
}
// if statements to sort input
if (input >= 0 && input <=9)
{
numbers[index] = input;
}
}
// call count function
count(numbers, SIZE, counter);
// call print function
print(counter);
return 0;
}
// counter function
void count(int numbers[], int SIZE, int counter[])
{
// for loop of counter
for (int index = 0 ; index < 10 ; index++)
{
// for loop of numbers
for (int tracker=0 ; tracker < SIZE ; tracker++)
{
// if statement to count each number
if (index == numbers[tracker])
{
counter[index]++;
}
}
}
return;
}
// print function
void print(int counter[])
{
// for loop to print each element
for (int index=0 ; index < 10 ; index++)
{
// if statement to only print numbers that were entered
if (counter[index] > 0)
{
cout << "You entered " << counter[index] << ", " << index << "(s)" << endl;
}
}
return;
}
@nbro, C++는 훌륭한 시작 언어입니다. – David
지식이 풍부한 선생님이 계시다면, OP는 분명히 그렇지 않습니다. 그는 또한 선택의 여지가 없을 것입니다. – Puppy
@David 그건 당신 의견입니다. – nbro