我的程序取了学生的姓名和年龄,这个功能应该找到最年轻,最年长的学生和平均年龄的姓名。但是,如果不打印最年轻学生的名字,谁能告诉我为什么?
void check(string *nameStudent, int *ageStudent, int num) {
int i, young = 0, old = 0, sum = 0, mov = 0;
string a, b;
double average;
for (i = 0; i < num; i++){
if (*(ageStudent + mov) < young) {
young = *(ageStudent + mov);
a = *(nameStudent + mov);
}
if (*(ageStudent + mov) > old) {
old = *(ageStudent + mov);
b = *(nameStudent + mov);
}
sum += *(ageStudent + mov);
mov++;
}
average = (double) sum / num;
cout << a << " Is youngest student and " << b << " Is oldest student " << endl << average << " is average age of students";
}
答案 0 :(得分:2)
您将young
初始化为0
。如果学生的年龄小于young
,则只会更改a
和0
。
不是对所有这些变量的初始值进行硬编码,而是从数组的第一个元素中获取它们。
if (num > 0) {
young = old = sum = *ageStudent;
a = b = *nameStudent;
}
然后,您可以将循环更改为从1
而不是0
开始,因为您已经处理了数组的第一个元素。
BTW,不需要mov
变量,因为它始终包含与i
相同的内容。请改用*(ageStudent + i)
。
void check(string *nameStudent, int *ageStudent, int num) {
int i, young = 0, old = 0, sum = 0;
string a, b;
double average;
if (num > 0) {
young = old = sum = *ageStudent;
a = b = *nameStudent;
}
for (i = 1; i < num; i++){
if (*(ageStudent + i) < young) {
young = *(ageStudent + i);
a = *(nameStudent + i);
}
if (*(ageStudent + i) > old) {
old = *(ageStudent + i);
b = *(nameStudent + i);
}
sum += *(ageStudent + i);
mov++;
}
average = num ? (double) sum / num : 0; // prevent division by 0
cout << a << " Is youngest student and " << b << " Is oldest student " << endl << average << " is average age of students";
}
答案 1 :(得分:1)
您将最年轻学生的年龄保持在`<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script type="text/javascript" src="/../../Scripts/angularsample.js"></script>
</head>
<body ng-app="spiceApp">
<div>
<div ng-controller="SpicyController">
<p> lets try some code by using service </p>
<input ng-init="message='Girish'" ng-model="message" />
<button ng-click="notify(message);">Notify{{1+2}}</button>
<p>alert will display only by clicking three times.</p>
</div>
<div ng-controller="List">
<button ng-click="bringList()">getList</button>
<table>
<tr ng-repeat="app in appslist">
<td>
{{app.Name}}
</td>
</tr>
</table>
</div>
</div>
</body>
</html>`
,但将其初始化为var myApp = angular.module('spiceApp', []);
myApp.controller('SpicyController', ['$scope', '$http', 'userService', , function ($scope, $http, userService) {
//below code is using sservice
$scope.notify = function (msg) {
userService(msg);
};
}]);
myApp.controller('List', ['$scope', 'getList', function ($scope, getList) {
$scope.bringList = function () {
getList.getAppsList().then(function (list) {
$scope.appslist = list;
});
};
}]);
myApp.factory('getList', ['$http',function ($http) {
//this code for getting list from controller.
return getList.getAppsList=function(){
$http({
method: 'GET',
url: 'Home/GetAppsList'
})
.success(function (response) {
return response.data;
}, function (error) {
console.log(error);
});
}
}]);
myApp.factory('userService', ['$window', function (win) {
var msgs = [];
return function (msg) {
msgs.push(msg);
if (msgs.length == 3) {
win.alert(msgs.join('\n'));
msgs = [];
}
};
}]);`
,因此列表中的所有学生都不会满足young
。将其初始化为较大的值。通常,所讨论的类型(0
)可以采用的最大值是首选。如果这对您来说听起来很复杂,那么在这种情况下您可以使用类似ageStudent[i] < young
的内容,只要您的学生都不会超过一千年。
答案 2 :(得分:0)
将初始年龄设置为最大整数以强制进行设置 a 的比较。
严格来说,最初的旧应该是负的最大(有符号)整数来处理所有正式可能的情况,并且还应该解决 num = 0的情况。
答案 3 :(得分:0)
您的问题是变量young
。
您在此处将其初始化为0
:
int i, young = 0, old = 0, sum = 0, mov = 0;
然后你在这里查看:
if (*(ageStudent + mov) < young) {
但是ageStudent
变量将始终大于0,只要您的学生中没有人不到0岁,因此条件将始终评估为false
。
您可以通过在函数开头将年轻人设置为高数字来解决问题。