有没有办法读取 4 个不同的数组以在 C 的返回函数中生成最小值和最大值
Posted
技术标签:
【中文标题】有没有办法读取 4 个不同的数组以在 C 的返回函数中生成最小值和最大值【英文标题】:Is there a way to read 4 different arrays to generate a minimum and maximum value in a return function in C 【发布时间】:2021-06-12 16:57:00 【问题描述】:我一直在开发 C 血压跟踪器/计算器应用程序,但在生成输入血压值的最小值和最大值时遇到了问题。 程序的预期输出是返回并显示平均收缩压和舒张压、最小收缩压和舒张压、最大收缩压和舒张压以及标准偏差收缩压和舒张压值。
此程序的最后更新
您好,您可能想知道这个程序是否已解决?最简洁的答案是不。但很长的答案是我必须从头开始,因为我的讲师告诉我,我正在制作的程序不符合给我的任务的要求。所以 4 天前是我不得不再次从零开始的时候,我很惊讶我花了 4 天时间才解决了具有正确要求的程序,尽管程序代码结构本身看起来很可怕。总之,原始程序已被废弃并重组为功能齐全的血压跟踪器应用程序。 结束
程序的预期功能
程序应该工作的方式是它从 4 个数组中获取用户输入,然后使用标准血压公式(通常从第二天开始)计算早上和下午的输入以生成输出从每个数组中提取,然后将输出分为早上和下午的读数,然后再通过 if 和 else if 语句来确定输出值是否与标准血压类别图匹配,这就是 calAvg 函数的工作原理.
对于 calMin 和 calMax 函数,它也可以通过从 4 个数组中获取输入来工作,但它不使用血压类别图表或血压公式来生成其输出,但它基本上读取 4 个数组,找到每个数组中的最小值或最大值,然后将 4 个数组生成的输出放入 2 个单独的数组中,称为 minSYS、minDYS(calMin 函数)、maxSYS 和 maxDYS(calMax 函数)。
将它们放入 2 个单独的数组后,开始读取 2 个数组的过程,以便找到 Systolic 和 Diastolic 中的最小值和最大值。
实际输出
但程序显示的实际输出是这样的:(当输入 150 Systolic & 50 Diastolic 时)
收缩压平均值:150 mmHG 收缩压最小值:0 mmHG 收缩压最大值:12584792 mmHG 舒张压平均值:50 mmHG 舒张压最小值:0 mmHG 舒张压最大值:268501009 mmHG
更新:
在做了更多实验后,输出没有显示我预期的结果,而是显示了这个。而且我已经更改了最小和最大数组的 SIZE,因为这样做没有意义。 输入 Systolic 150 & Diastolic 50 时的当前实际输出: 收缩压平均值:150 mmHG 收缩压最小值:0 mmHG 收缩压最大值:8140608 mmHG 舒张压平均值:50 mmHG 舒张压最小值:0 mmHG 舒张压最大值:6421796 mmHG
这是我用来生成该输出的代码:
int calMin(int readMornSYS[SIZE], int readAfterSYS[SIZE], int readMornDYS[SIZE], int readAfterDYS[SIZE], int minSYS[SIZE], int minDYS[SIZE]) //wip
int index, smallest1, smallest2, smallest3, smallest4;
int smallestSYSTotal = 0, smallestDYSTotal = 0;
//INITIALIZING smallest variables
smallest1 = readMornSYS[0];
smallest2 = readAfterSYS[0];
smallest3 = readMornDYS[0];
smallest4 = readAfterDYS[0];
for(index=0; index < SIZE; index++)
minSYS[0] = smallest1;
minSYS[1] = smallest2;
minDYS[0] = smallest3;
minDYS[1] = smallest4;
if(smallestSYSTotal > minSYS[index])
smallestSYSTotal = minSYS[index];
if(smallestDYSTotal > minDYS[index])
smallestDYSTotal = minDYS[index];
printf("Systolic Minimum : %d mmHG\n", smallestSYSTotal);
return smallestDYSTotal;
我尝试将数组链接到一个变量中,然后尝试使用标准方法来查找和输出数组的最小值和最大值,但到目前为止,这些方法都没有奏效。而且我也尝试过更改索引值,但这样做之后,整个程序崩溃了。
这是程序的完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 7
int calSTDDEV(int readMornSYS[SIZE], int readAfterSYS[SIZE],int readMornDYS[SIZE], int readAfterDYS[SIZE]);
int calMax(int readMornSYS[SIZE], int readAfterSYS[SIZE], int readMornDYS[SIZE], int readAfterDYS[SIZE], int maxSYS[4], int maxDYS[4]);
int calMin(int readMornSYS[SIZE], int readAfterSYS[SIZE], int readMornDYS[SIZE], int readAfterDYS[SIZE], int minSYS[4], int minDYS[4]);
int calAvg(int readMornSYS[SIZE], int readAfterSYS[SIZE], int readMornDYS[SIZE], int readAfterDYS[SIZE]);
void displayHBPT(int avgHBP, int minHBP, int maxHBP, int stddevHBP);
void inputRead(int readMornSYS[SIZE], int readAfterSYS[SIZE],int readMornDYS[SIZE], int readAfterDYS[SIZE]);
int main()
int readMornSYS[SIZE], readMornDYS[SIZE];
int readAfterSYS[SIZE], readAfterDYS[SIZE];
int minSYS[4], minDYS[4];
int maxSYS[4], maxDYS[4];
int minHBP, avgHBP, maxHBP, stddevHBP;
char userInput;
//Title of the program
printf("HBPTracker Application\n");
printf("==================================\n");
//Disclaimer of the program
printf("DISCLAIMER: The HBPTracker Application is only capable of recording data for a week.\n");
do
//This is the main menu of the program where you can decide to Start or Quit the program
printf("==================================\n");
printf("Do you wish to start<S> or quit<Q>: ");
scanf(" %c", &userInput);
//If the input is Start the input function will prompt out
if(userInput != 'Q')
//The input function reads all of the 4 arrays
inputRead(readMornSYS,readAfterSYS,readMornDYS,readAfterDYS);
//The average function calculates the 4 arrays and then returns 2 values
avgHBP = calAvg(readMornSYS,readAfterSYS,readMornDYS,readAfterDYS);
//The minimum function scans for the smallest value in each of the 4 arrays
//and then the output of the 4 arrays are put into two separate arrays to return two minimum values
minHBP = calMin(readMornSYS, readAfterSYS, readMornDYS, readAfterDYS, minSYS, minDYS);
//The maximum function scans for the biggest value in each of the 4 arrays
//and then the output of the 4 arrays are put into two separate arrays to return two maximum values
maxHBP = calMax(readMornSYS, readAfterSYS, readMornDYS, readAfterDYS, maxSYS, maxDYS);
//The display function mainly displays the second return value of each of the other functions
displayHBPT(avgHBP,minHBP,maxHBP,stddevHBP);
while(userInput != 'Q');
printf("==================================\n");
printf("You have exited the program.\n");
printf("==================================\n");
return 0;
void displayHBPT(int avgHBP, int minHBP, int maxHBP, int stddevHBP)
printf("Diastolic Average : %d mmHG\n", avgHBP);
printf("Diastolic Minimum : %d mmHG\n", minHBP);
printf("Diastolic Maximum : %d mmHG\n", maxHBP);
//printf("This is where Standard Deviation would be %d\n",stddevHBP);
void inputRead(int readMornSYS[SIZE], int readAfterSYS[SIZE],int readMornDYS[SIZE], int readAfterDYS[SIZE])
int index, day = 1;
char userInput;
for(index=0; index < SIZE; index++)
printf("==================================\n");
printf("Day : %d\n", day);
printf("Enter Morning Blood Systolic[Upper] : ");
scanf("%d", &readMornSYS[index]);
printf("Enter Morning Blood Diastolic[Lower] : ");
scanf("%d", &readMornDYS[index]);
printf("Enter Afternoon Blood Systolic[Upper] : ");
scanf("%d", &readAfterSYS[index]);
printf("Enter Afternoon Blood Diastolic[Lower] : ");
scanf("%d", &readAfterDYS[index]);
printf("==================================\n");
printf("Do you wish to proceed to the next day?[Y/N] : ");
scanf(" %c", &userInput);
if(userInput == 'Y' && userInput != 'N')
day++;
else if(userInput == 'N')
break;
int calAvg(int readMornSYS[SIZE], int readAfterSYS[SIZE], int readMornDYS[SIZE], int readAfterDYS[SIZE])
int index;
int calBPSYS1=0,calBPSYS2=0, avgBPSYS1=0,avgBPSYS2=0, totalSYS=0;
int calBPDYS1=0,calBPDYS2=0, avgBPDYS1=0,avgBPDYS2=0, totalDYS=0;
for(index = 1; index < SIZE; index++)
//Average Blood Pressure Formula 7 Days: Start(Day 2)
//STEP 1 sum the values in each array
calBPSYS1 += readMornSYS[index];
calBPSYS2 += readAfterSYS[index];
calBPDYS1 += readMornDYS[index];
calBPDYS2 += readAfterDYS[index];
//STEP 2 divide the total of Systolic and Diastolic arrays by the index of readings
avgBPSYS1 = (avgBPSYS1+calBPSYS1)/index;
avgBPSYS2 = (avgBPSYS2+calBPSYS2)/index;
avgBPDYS1 = (avgBPDYS1+calBPDYS1)/index;
avgBPDYS2 = (avgBPDYS2+calBPDYS2)/index;
//STEP 3 add the average total of each array into their respective reading groups
//and then divide by the number of systolic arrays(2) and diastolic arrays(2)
totalSYS = (totalSYS + avgBPSYS1 + avgBPSYS2)/2;
totalDYS = (totalDYS + avgBPDYS1 + avgBPDYS2)/2;
if(totalSYS < 120 && totalDYS < 80)
printf("==================================\n");
printf("Your Blood Pressure is NORMAL\n");
printf("==================================\n");
printf("Systolic Average : %d mmHG\n",totalSYS);
return totalDYS;
else if(totalSYS >= 120 && totalSYS <= 129 && totalDYS < 80)
printf("==================================\n");
printf("Your Blood Pressure is ELEVATED\n");
printf("==================================\n");
printf("Systolic Average : %d mmHG\n",totalSYS);
return totalDYS;
else if(totalSYS >= 130 || totalSYS <= 139 || totalDYS >= 80 || totalDYS <= 89)
printf("==================================\n");
printf("You have HIGH BLOOD PRESSURE\n");
printf("\tHYPERTENSION STAGE 1\n");
printf("==================================\n");
printf("Systolic Average : %d mmHG\n",totalSYS);
return totalDYS;
else if(totalSYS >= 140 || totalDYS >= 90)
printf("==================================\n");
printf("You have HIGH BLOOD PRESSURE\n");
printf("\tHYPERTENSION STAGE 2\n");
printf("==================================\n");
printf("Systolic Average : %d mmHG\n",totalSYS);
return totalDYS;
else if(totalSYS > 180 || totalDYS > 100)
printf("==================================\n");
printf("Your Blood Pressure is TOO HIGH\n");
printf("Please consult a doctor immediately.\n");
printf("==================================\n");
printf("Systolic Average : %d mmHG\n",totalSYS);
return totalDYS;
int calMin(int readMornSYS[SIZE], int readAfterSYS[SIZE], int readMornDYS[SIZE], int readAfterDYS[SIZE], int minSYS[4], int minDYS[4]) //wip
int index, smallest1, smallest2, smallest3, smallest4;
int smallestSYSTotal = 0, smallestDYSTotal = 0;
//INITIALIZING smallest variables
smallest1 = readMornSYS[0];
smallest2 = readMornDYS[0];
smallest3 = readMornDYS[0];
smallest4 = readAfterDYS[0];
for(index=1; index < SIZE; index++)
if(readMornSYS[index]<smallest1) //checking for smallest value in readMornSYS array
smallest1 = readMornSYS[index];
minSYS[index] = smallest1;
if(readAfterSYS[index]<smallest2) //checking for smallest value in readAfterSYS array
smallest2 = readAfterSYS[index];
minSYS[index] = smallest2;
if(readMornDYS[index]<smallest3) //checking for smallest value in readMornDYS array
smallest3 = readMornDYS[index];
minDYS[index] = smallest3;
if(readAfterDYS[index]<smallest4) //checking for smallest value in readAfterDYS array
smallest4 = readAfterDYS[index];
minDYS[index] = smallest4;
for(index=1; index < 4; index++)
if(minSYS[index]<smallestSYSTotal)
smallestSYSTotal = minSYS[index];
printf("Systolic Minimum : %d mmHG\n", smallestSYSTotal);
for(index=0; index < 4; index++)
if(minDYS[index]<smallestDYSTotal)
smallestDYSTotal = minDYS[index];
return smallestDYSTotal;
int calMax(int readMornSYS[SIZE], int readAfterSYS[SIZE], int readMornDYS[SIZE], int readAfterDYS[SIZE], int maxSYS[4], int maxDYS[4]) //wip
int index, biggest1,biggest2,biggest3,biggest4;
int biggestSYSTotal = 0, biggestDYSTotal = 0;
//INITIALIZING biggest variables
biggest1 = readMornSYS[0];
biggest2 = readAfterSYS[0];
biggest3 = readMornDYS[0];
biggest4 = readAfterDYS[0];
for(index=0; index < SIZE; index++)
if(readMornSYS[index]>biggest1) //checking for biggest value in readMornSYS array
biggest1 = readMornSYS[index];
maxSYS[index] = biggest1;
if(readAfterSYS[index]>biggest2) //checking for biggest value in readAfterSYS array
biggest2 = readAfterSYS[index];
maxSYS[index] = biggest2;
if(readMornDYS[index]>biggest3) //checking for biggest value in readMornDYS array
biggest3 = readMornDYS[index];
maxDYS[index] = biggest3;
if(readAfterDYS[index]>biggest4) //checking for biggest value in readAfterDYS array
biggest4 = readAfterDYS[index];
maxDYS[index] = biggest4;
for(index=0; index < 4; index++)
if(maxSYS[index]>biggestSYSTotal)
biggestSYSTotal = maxSYS[index];
printf("Systolic Maximum : %d mmHG\n",biggestSYSTotal);
for(index=0; index < 4; index++)
if(maxDYS[index]>biggestDYSTotal)
biggestDYSTotal = maxDYS[index];
return biggestDYSTotal;
【问题讨论】:
在函数calAvg()
中,您有for(index = 1; ...
(与calMin()
不同)您已将总计变量初始化为0。元素[0]
会发生什么情况?您似乎也有平均和报告inside求和循环。
@WeatherVane 哦,那是因为血压公式说要跳过第一个输入,即 index[0] 所以这就是为什么我将 index = 1 放在 for 循环中,但对于总数'部分我认为这是将总数初始化为 0 的标准?
Please do not post images of texts (the output in this case) because they are hard to use. 文本应作为文本直接发布在您的问题中。
旁白:我认为这是将总数初始化为 0 的标准 - 如果您将它们设置为元素 [0]
并从元素 [1]
循环,就像我想的那样你可能已经打算好了。
你打破了数组长度为 4 的数组边界 ==> 崩溃? minSYS[index] = smallest1;
不应该在 循环之后和(例如)inSYS[0] = smallest1;
吗?
【参考方案1】:
您打破了传递给函数的数组长度 [4]
的界限,在循环中将它们索引到 < SIZE
即 7
。
我建议你将一些语句移出循环,然后是这样的:
minSYS[0] = smallest1;
minSYS[1] = smallest2;
minSYS[2] = smallest3;
minSYS[3] = smallest4;
等等...和其他功能类似
【讨论】:
以上是关于有没有办法读取 4 个不同的数组以在 C 的返回函数中生成最小值和最大值的主要内容,如果未能解决你的问题,请参考以下文章