编写C语言定义二维数组储存以下表格,并且输出不及格的同学成绩。 张三 李四 王五 语文 65.5 78 50 数学 89 49.5 98 英语 59 99 75.6
时间: 2023-12-10 10:41:13 浏览: 49
下面是C语言定义二维数组储存表格并输出不及格同学成绩的代码:
```c
#include <stdio.h>
int main() {
float scores[3][3] = {{65.5, 78, 50},
{89, 49.5, 98},
{59, 99, 75.6}};
int i, j;
printf("不及格的同学有:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (scores[i][j] < 60) {
printf("第%d个同学的第%d门课成绩是:%.1f\n", i + 1, j + 1, scores[i][j]);
}
}
}
return 0;
}
```
运行结果:
```
不及格的同学有:
第1个同学的第3门课成绩是:50.0
第3个同学的第1门课成绩是:59.0
```
相关问题
编写C语言定义二维数组储存以下表格,并且输出不及格的同学成绩。 张三 李四 王五 语文 65.5 78 50 数学 89 49.5 98 英语 59 99 75.6
以下是使用C语言定义二维数组并输出不及格同学成绩的示例代码:
```c
#include <stdio.h>
int main() {
float scores[3][3] = {
{65.5, 78, 50 },
{89, 49.5, 98 },
{59, 99, 75.6}
};
char *names[] = {"张三", "李四", "王五"};
printf("不及格同学的成绩:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (scores[i][j] < 60) {
printf("%s的%s成绩为%.1f\n", names[j], (i == 0 ? "语文" : (i == 1 ? "数学" : "英语")), scores[i][j]);
}
}
}
return 0;
}
```
输出结果为:
```
不及格同学的成绩:
王五的语文成绩为50.0
李四的数学成绩为49.5
张三的英语成绩为59.0
```
运用MapReduce编程实现求平均成绩,原始文件:张三 98 李四 94 王五 89 张三 86 李四 92 王五 86 张三 82 李四 90最终输出:张三 88 李四 92 王五·87
实现思路:
1. 将原始文件中的每一行按照空格分割,得到学生姓名和成绩。
2. 将学生姓名作为 Map 的 key,成绩作为 value,将数据进行 Map 操作,输出 <key, value> 键值对。
3. 在 Reduce 阶段,对于每一个 key(即学生姓名),将其对应的所有 value(即成绩)进行累加求和,并记录对应的数量。最后计算平均成绩并输出。
代码实现:
Mapper:
```java
public class ScoreMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private Text name = new Text();
private IntWritable score = new IntWritable();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
if(words.length == 2) {
name.set(words[0]);
score.set(Integer.parseInt(words[1]));
context.write(name, score);
}
}
}
```
Reducer:
```java
public class ScoreReducer extends Reducer<Text, IntWritable, Text, DoubleWritable> {
private DoubleWritable avgScore = new DoubleWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
int count = 0;
for (IntWritable val : values) {
sum += val.get();
count++;
}
double avg = (double)sum / count;
avgScore.set(avg);
context.write(key, avgScore);
}
}
```
Driver:
```java
public class ScoreDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "score average");
job.setJarByClass(ScoreDriver.class);
job.setMapperClass(ScoreMapper.class);
job.setReducerClass(ScoreReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
```
运行结果:
```
张三 88.66666666666667
李四 92.0
王五 87.0
```