数组内部函数
array inside function
创建一个函数来显示数组。
#include<stdio.h>
#define SIZE 5
int scoreList[SIZE];
int i;
int findTotal(int scoreList[SIZE], int inSmall, int inLarge){
int total =0;
int index;
for (index = 0; index < SIZE; index++)
{
if (scoreList[index]!= inSmall && scoreList[index]!= inLarge)
total = total + scoreList[index];
}
return total;
}
int findLarge(int scoreList[SIZE]){
int index;
int largest = scoreList[0];
for (index=1; index < SIZE; index++)
{
if (scoreList[index] > largest)
largest = scoreList[index];
}
return largest;
}
int findSmall(int scoreList[SIZE]){
int index;
int smallest = scoreList[0];
for (index=1; index < SIZE; index++)
{
if (scoreList[index] < smallest)
smallest = scoreList[index];
}
return smallest;
}
int main(){
const char* question[] = { "I don't talk a lot", "I have people around me", "I love nature", "I talk about science", "And bla bla bla" };
// char question[SIZE][100] = {"I don't talk a lot","why this is good","one two","fourth","fifth"}; // If you don't wish to use pointer, You will have to use a 2D char array that will store the total number of element and second one will store length of each element (here 100)
for(i=0; i<SIZE; i++){
printf("%s :\nYour Score: ", question[i]);
scanf("%d", &scoreList[i]);
}
int smallest = findSmall(scoreList);
int largest = findLarge(scoreList);
int total = findTotal(scoreList, smallest, largest);
printf("smallest: %d, largest: %d, total: %d\n", smallest, largest, total);
return 0;
}
我的系统上有 运行 它。工作正常!
#include<stdio.h>
#define MAX_SIZE_ALLOTED_TO_EACH_QUESTION 100
#define NUMBER_OF_QUESTIONS 10
int scoreList[NUMBER_OF_QUESTIONS]; // This array will contain the individual score for each answers to respective questions
int i;
void Extroversion(int scoreList[]){
// Let formula for this be E = 20 +(1)___-(3)___+(2)___-(4)___
int E = 20 + (scoreList[1] + scoreList[2]) - (scoreList[3] + scoreList[4]);
printf("\nExtroversion is the personality trait of seeking fulfillment from sources outside the self or in community. High scorers tend to be very social while low scorers prefer to work on their projects alone.\nYour score for this trait is: %d\n\n", E);
}
void Agreeableness(int scoreList[]){
// Let formula for this be A = 14 -(2)___+(5)___-(3)___+(6)___
int A = 20 + (scoreList[5] + scoreList[6]) - (scoreList[2] + scoreList[3]);
printf("\nAgreeableness reflects much individuals adjust their behavior to suit others. High scorers are typically polite and like people. Low scorers tend to 'tell it like it is'.\nYour score for this trait is: %d\n\n", A);
}
/*
* Similarily for Conscientiousness, Neuroticism and Openness_to_Experience
*/
int main(){
const char question[NUMBER_OF_QUESTIONS][MAX_SIZE_ALLOTED_TO_EACH_QUESTION] = { "1. Am the life of the party.", "2. Feel little concern for others.", "3. Am always prepared.", "4. Get stressed out easily.", "5. Have a rich vocabulary.", "6. Don't talk a lot.", "7. Am interested in people.", "8. Leave my belongings around.", "9. Am relaxed most of the time.", "10. Have difficulty understanding abstract ideas." };
for(i=0; i<NUMBER_OF_QUESTIONS; i++){
printf("%s :\nYour Score: ", question[i]);
scanf("%d", &scoreList[i]); // Here each element is storing the value between 0-5 for their corresponsding question
}
Extroversion(scoreList);
Agreeableness(scoreList);
// Conscientiousness(scoreList);
// Neuroticism(scoreList);
// Openness_to_Experience(scoreList);
return 0;
}
我认为你把代码看一遍会很好。我在代码中使用 //
和 /* */
提到了注释。正确阅读评论。我已经在我的系统上检查了 10 个问题的代码,所以如果你按照我写的模式完成,就不会有错误。并且不要忘记查看公式。数组索引需要按照你分享的原pdf里面的公式。
创建一个函数来显示数组。
#include<stdio.h>
#define SIZE 5
int scoreList[SIZE];
int i;
int findTotal(int scoreList[SIZE], int inSmall, int inLarge){
int total =0;
int index;
for (index = 0; index < SIZE; index++)
{
if (scoreList[index]!= inSmall && scoreList[index]!= inLarge)
total = total + scoreList[index];
}
return total;
}
int findLarge(int scoreList[SIZE]){
int index;
int largest = scoreList[0];
for (index=1; index < SIZE; index++)
{
if (scoreList[index] > largest)
largest = scoreList[index];
}
return largest;
}
int findSmall(int scoreList[SIZE]){
int index;
int smallest = scoreList[0];
for (index=1; index < SIZE; index++)
{
if (scoreList[index] < smallest)
smallest = scoreList[index];
}
return smallest;
}
int main(){
const char* question[] = { "I don't talk a lot", "I have people around me", "I love nature", "I talk about science", "And bla bla bla" };
// char question[SIZE][100] = {"I don't talk a lot","why this is good","one two","fourth","fifth"}; // If you don't wish to use pointer, You will have to use a 2D char array that will store the total number of element and second one will store length of each element (here 100)
for(i=0; i<SIZE; i++){
printf("%s :\nYour Score: ", question[i]);
scanf("%d", &scoreList[i]);
}
int smallest = findSmall(scoreList);
int largest = findLarge(scoreList);
int total = findTotal(scoreList, smallest, largest);
printf("smallest: %d, largest: %d, total: %d\n", smallest, largest, total);
return 0;
}
我的系统上有 运行 它。工作正常!
#include<stdio.h>
#define MAX_SIZE_ALLOTED_TO_EACH_QUESTION 100
#define NUMBER_OF_QUESTIONS 10
int scoreList[NUMBER_OF_QUESTIONS]; // This array will contain the individual score for each answers to respective questions
int i;
void Extroversion(int scoreList[]){
// Let formula for this be E = 20 +(1)___-(3)___+(2)___-(4)___
int E = 20 + (scoreList[1] + scoreList[2]) - (scoreList[3] + scoreList[4]);
printf("\nExtroversion is the personality trait of seeking fulfillment from sources outside the self or in community. High scorers tend to be very social while low scorers prefer to work on their projects alone.\nYour score for this trait is: %d\n\n", E);
}
void Agreeableness(int scoreList[]){
// Let formula for this be A = 14 -(2)___+(5)___-(3)___+(6)___
int A = 20 + (scoreList[5] + scoreList[6]) - (scoreList[2] + scoreList[3]);
printf("\nAgreeableness reflects much individuals adjust their behavior to suit others. High scorers are typically polite and like people. Low scorers tend to 'tell it like it is'.\nYour score for this trait is: %d\n\n", A);
}
/*
* Similarily for Conscientiousness, Neuroticism and Openness_to_Experience
*/
int main(){
const char question[NUMBER_OF_QUESTIONS][MAX_SIZE_ALLOTED_TO_EACH_QUESTION] = { "1. Am the life of the party.", "2. Feel little concern for others.", "3. Am always prepared.", "4. Get stressed out easily.", "5. Have a rich vocabulary.", "6. Don't talk a lot.", "7. Am interested in people.", "8. Leave my belongings around.", "9. Am relaxed most of the time.", "10. Have difficulty understanding abstract ideas." };
for(i=0; i<NUMBER_OF_QUESTIONS; i++){
printf("%s :\nYour Score: ", question[i]);
scanf("%d", &scoreList[i]); // Here each element is storing the value between 0-5 for their corresponsding question
}
Extroversion(scoreList);
Agreeableness(scoreList);
// Conscientiousness(scoreList);
// Neuroticism(scoreList);
// Openness_to_Experience(scoreList);
return 0;
}
我认为你把代码看一遍会很好。我在代码中使用 //
和 /* */
提到了注释。正确阅读评论。我已经在我的系统上检查了 10 个问题的代码,所以如果你按照我写的模式完成,就不会有错误。并且不要忘记查看公式。数组索引需要按照你分享的原pdf里面的公式。