指针函数的应用:使用一个3*4的数组存储学生的成绩(每个学生有4门课程),要求用户输入学生序号(即第几个学生,第一个学生的序号为1)后能输出该生的全部成绩(成绩保留一位小数)。要求定义函数,且使用指针变量。
时间: 2024-06-10 16:09:31 浏览: 10
```
#include <stdio.h>
void print_score(float *p, int n)
{
printf("第%d个学生的成绩为:", n);
for(int i = 0; i < 4; i++)
{
printf("%.1f ", *(p + (n - 1) * 4 + i));
}
printf("\n");
}
int main()
{
float score[3][4];
for(int i = 0; i < 3; i++)
{
printf("请输入第%d个学生的成绩:\n", i + 1);
for(int j = 0; j < 4; j++)
{
scanf("%f", &score[i][j]);
}
}
int n;
printf("请输入学生序号:\n");
scanf("%d", &n);
print_score(*score, n);
return 0;
}
```
注:指针变量p指向数组score的首地址,通过指针变量p可以访问数组元素。指针变量p+(n-1)*4+i表示第n个学生的第i门课的成绩。
相关问题
9.编写程序:有3个学生,每个学生有4门课程的成绩。要求用户输入学生 序号以后,能输出该学生的全部成绩。用指针函数来实现 学...
以下是用指针函数实现该程序的代码:
```c
#include <stdio.h>
void input_scores(int *scores, int num);
void output_scores(int *scores, int num);
int main()
{
int scores[3][4];
int student_num;
// 输入每个学生的成绩
for (int i = 0; i < 3; i++) {
printf("请输入第%d个学生的4门成绩:\n", i+1);
input_scores(scores[i], 4);
}
// 输入要查询的学生序号
printf("请输入要查询的学生序号(1-3):\n");
scanf("%d", &student_num);
// 输出该学生的成绩
printf("该学生的成绩为:\n");
output_scores(scores[student_num-1], 4);
return 0;
}
// 输入学生的成绩
void input_scores(int *scores, int num)
{
for (int i = 0; i < num; i++) {
scanf("%d", &scores[i]);
}
}
// 输出学生的成绩
void output_scores(int *scores, int num)
{
for (int i = 0; i < num; i++) {
printf("%d ", scores[i]);
}
printf("\n");
}
```
这个程序使用了二维数组 `scores` 存储学生的成绩,使用指针函数 `input_scores` 和 `output_scores` 分别输入和输出学生的成绩。在 `main` 函数中,先输入每个学生的成绩,然后输入要查询的学生序号,最后输出该学生的成绩。注意,由于数组下标从 0 开始,所以要查询的学生序号需要减去 1。
有5个学生的成绩,每个学生有4门课程,要求在用户输入学生序号以后,能输出该学生的全部成绩(用返回指针值的函数来实现)。
### 回答1:
可以定义一个二维数组来存储5个学生的4门课程成绩,然后定义一个返回指针值的函数,当用户输入学生序号时,函数返回该学生的全部成绩。
具体实现可以参考以下代码:
```c++
#include <iostream>
using namespace std;
int* getScore(int stuNum, int scores[][4]) {
return scores[stuNum - 1];
}
int main() {
int scores[5][4] = {{80, 90, 85, 95}, {70, 75, 80, 85}, {90, 95, 92, 88}, {85, 80, 90, 95}, {78, 85, 90, 92}};
int stuNum;
cout << "请输入学生序号(1-5):";
cin >> stuNum;
int* stuScores = getScore(stuNum, scores);
cout << "学生" << stuNum << "的成绩为:";
for (int i = ; i < 4; i++) {
cout << stuScores[i] << " ";
}
cout << endl;
return ;
}
```
运行程序后,用户输入学生序号,程序会调用getScore函数返回该学生的全部成绩,并输出到屏幕上。
### 回答2:
题目要求实现一个能够根据用户输入的学生序号来输出该学生全部成绩的程序。这里可以使用函数来实现。
首先,需要定义一个包含成绩信息的二维数组,可以使用int类型存储。数组可以定义为全局变量或者在main函数中直接声明。
其次,需要实现一个函数,通过返回指针值来获取对应学生的成绩。函数的参数应该包括学生序号和成绩数组。其中,学生序号可以在函数外部通过输入获得,成绩数组是全局变量,可以直接在函数中使用。函数的返回值应该是一个指向int类型数组的指针。
最后,在main函数中实现用户交互部分,即输入学生序号,并调用函数输出对应学生的成绩。需要注意的是,输入的序号需要进行合法性检查,确保在数组范围内。
下面给出一个简单的实现示例:
```c++
#include <iostream>
using namespace std;
const int STUDENT_NUM = 5;
const int COURSE_NUM = 4;
int scores[STUDENT_NUM][COURSE_NUM] = {
{80, 70, 90, 85},
{75, 85, 72, 87},
{90, 80, 85, 92},
{68, 78, 81, 76},
{88, 90, 87, 91}
};
int* getScore(int num, int (*arr)[COURSE_NUM]) {
if (num >= 1 && num <= STUDENT_NUM) {
return arr[num - 1];
} else {
cout << "Invalid input!" << endl;
return nullptr;
}
}
int main() {
int num;
cout << "Please enter student number(1-5): ";
cin >> num;
int* pScore = getScore(num, scores);
if (pScore != nullptr) {
cout << "Scores: ";
for (int i = 0; i < COURSE_NUM; i++) {
cout << pScore[i] << " ";
}
cout << endl;
}
return 0;
}
```
在上面的代码中,全局变量scores存储了五个学生的成绩信息。函数getScore根据输入的学生序号返回对应的成绩数组指针。在main函数中,用户输入学生序号后调用getScore函数获取该学生成绩,并输出到控制台。代码实现简单易懂,可以满足题目需求。
### 回答3:
题目要求实现的主要功能是在用户输入学生序号以后,输出该学生的全部成绩。为了实现这个功能,我们可以考虑用一个二维数组来存储所有学生的成绩。数组中的行表示学生,列表示课程,因此二维数组的大小应该是5行4列。
接下来,我们需要实现一个函数来根据用户输入的学生序号返回该学生的成绩。这个函数需要接受两个参数:二维数组和学生序号。函数内部需要遍历数组,找到指定学生的成绩,并使用指针返回学生的成绩。
下面是代码实现:
```c++
#include <iostream>
using namespace std;
void getScores(int (*scores)[4], int studentNum, int* result) {
for (int i = 0; i < 4; i++) {
result[i] = scores[studentNum][i];
}
}
int main() {
int scores[5][4] = {
{80, 85, 90, 87},
{75, 88, 92, 80},
{90, 82, 85, 91},
{94, 89, 93, 95},
{86, 91, 88, 90}
};
int studentNum;
cout << "请输入学生序号:" << endl;
cin >> studentNum;
int result[4];
getScores(scores, studentNum, result);
cout << "学生 " << studentNum << " 的成绩为:";
for (int i = 0; i < 4; i++) {
cout << result[i] << " ";
}
cout << endl;
return 0;
}
```
在上面的代码中,我们定义了一个名为getScores的函数,用来获取指定学生的成绩。该函数接收三个参数:scores二维数组、学生序号和表示结果的指针。在函数内部,我们使用循环遍历了scores数组中指定学生的成绩,并将结果存储在了result指针所指向的数组中。
在主函数中,我们首先定义了5个学生4门课程的二维数组scores。然后,我们提示用户输入学生序号,并用cin将输入的值存储在studentNum变量中。接着,我们调用getScores函数获取指定学生的成绩,并将结果存储在了result数组中。最后,我们将结果输出到屏幕上。
注意,由于getScores函数使用了指针返回结果,我们需要提前定义一个数组来存储函数返回的结果。在本例中,我们定义了一个名为result的数组,用来存储学生的成绩。由于我们知道每个学生都有4门课程,因此我们在定义result时也指定了数组的大小为4。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)