编写C语言定义二维数组储存以下表格,并且输出不及格的同学成绩。 张三 李四 王五 语文 65.5 78 50 数学 89 49.5 98 英语 59 99 75.6
时间: 2023-12-10 12:41:14 浏览: 28
以下是使用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
```
编写c语言程序,员工工资管理系统 基本工资 奖金 总工资 张三 4500 500 李四 6500 800 王五 5500 600 陈六 6000 500 (1) 使用二维数组,存储上面表格中的数据。其中,总工资需要编程计算并填入对应数组元素中,,并输出。 (2) 显示所有基本工资低于6000的员工姓名
以下是一个实现该员工工资管理系统的 C 语言程序:
```c
#include <stdio.h>
#include <string.h>
#define SIZE 4
struct Employee {
char name[10];
float basic_salary;
float bonus;
float total_salary;
};
int main() {
struct Employee employees[SIZE] = {
{"张三", 4500, 500, 0},
{"李四", 6500, 800, 0},
{"王五", 5500, 600, 0},
{"陈六", 6000, 500, 0}
};
// 计算总工资并填入数组中
for (int i = 0; i < SIZE; i++) {
employees[i].total_salary = employees[i].basic_salary + employees[i].bonus;
}
// 输出所有员工信息
printf("员工姓名\t基本工资\t奖金\t总工资\n");
for (int i = 0; i < SIZE; i++) {
printf("%s\t%.2f\t%.2f\t%.2f\n", employees[i].name, employees[i].basic_salary, employees[i].bonus, employees[i].total_salary);
}
// 显示所有基本工资低于6000的员工姓名
printf("\n基本工资低于6000的员工姓名:\n");
for (int i = 0; i < SIZE; i++) {
if (employees[i].basic_salary < 6000) {
printf("%s\n", employees[i].name);
}
}
return 0;
}
```
程序中使用了一个结构体 `Employee` 来表示每一个员工,其中包含员工的姓名、基本工资、奖金和总工资。使用一个二维数组也可以存储这些数据,但是结构体可以更好地组织数据并提高代码的可读性。
程序首先初始化了一个包含四个员工的数组 `employees`,然后计算每个员工的总工资并将其填入数组中。最后输出所有员工的信息,并显示所有基本工资低于6000的员工的姓名。