我必须编写一个程序,其中main调用其他函数来测试一系列数字(如果有的话)是否小于一个数字,如果所有系列的数字都在两个限制之间,如果有的话是负数.我的代码返回值为1表示true,0表示false表示,但是赋值会将它们打印为"true"或"false".我不知道如何将bool答案从printf打印为字符串.我使用if(atl == false)printf("false"); 在我的at_least.c和main.c中,它只返回一个true或false的长字符串(例如:truetruetrue ....).我不确定这是不是正确的编码,我把它放在错误的位置,或者我需要使用其他一些代码.
这是我的main.c:
#include "my.h"
int main (void)
{
int x;
int count = 0;
int sum = 0;
double average = 0.0;
int largest = INT_MIN;
int smallest = INT_MAX;
bool atlst = false;
bool bet = true;
bool neg = false;
int end;
while ((end = scanf("%d",&x)) != EOF)
{
sumall(x, &sum); //calling function sumall
count++;
larger_smaller(x, &largest, &smallest); //calling function larger_smaller
if (atlst == false)
at_least(x, &atlst); //calling function at_least if x < 50 …Run Code Online (Sandbox Code Playgroud) 我收到错误:
main.o(.text+0x1ed): In function `main':
: undefined reference to `avergecolumns'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
当我gcc*.o.我不太清楚导致这个错误的原因.其他海报已经解释了它没有找到功能或功能是空的.如果有人可以澄清或改进,那将非常感谢!
这是我的函数代码(我试图计算2D数组中列的平均值):
#include "my.h"
void averagecolumns (int x, int y, int** a)
{
int i;
int j;
float sum;
float colAvg;
sum = 0;
colAvg = 0;
printf("i. The column averages are: \n");
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
sum += a[i][j];
colAvg = sum / (float)x;
}
printf("Column: %3d, Average: %6.2f", j, colAvg); …Run Code Online (Sandbox Code Playgroud) 我一直遇到分段错误,但我不确定这意味着什么或如何分辨它是什么原因(我对编程和C非常新).在main.c调用的这个函数中,我需要确定二维数组的eacg行中最小数字的索引.
这是我的代码:
#include "my.h"
void findfirstsmall (int x, int y, int** a)
{
int i;
int j;
int small;
small = y - 1;
printf("x = %3d, y = %3d\n", x, y); //trying to debug
printf("f. The first index of the smallest number is: \n");
for(i = 0; i < x; i++)
{
for(j = 0; j < y; i++) <---------- needs to be j, for any future readers
{
if(a[i][small] > a[i][j])
small = j;
printf("small = %3d\n", small); …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,在main中调用一个函数来创建一个节点来构建一个链表.程序从要放置在节点中的文件中读取字符.程序运行正常,直到它进入创建节点的函数.我不确定是否存在逻辑或语法错误或其他错误.抱歉长度,但错误距离一开始并不远.另外,如果您需要我的头文件,请告诉我.代码来自我的C书:计算机科学:结构化编程方法使用C,第三版.任何帮助(并解释我的错误)将不胜感激!
谢谢!
这是main.c:
#include "my.h"
int main (int argc, char* argv[])
{
int y;
NODE* pList;
NODE* pPre;
NODE* pNew;
DATA item;
FILE* fpntr;
int closeResult;
pList = NULL;
fpntr = fopen(argv[1], "r"); // open file
if(!fpntr)
{
printf("Could not open input file.\n");
exit (101);
}
else printf("The file opened.\n");
printf("starting InNode.\n"); //testing to see if this is working
while((y = fscanf(fpntr, "%c", &(item.key))) != EOF)
pList = InNode(pList, pPre, item);
printf("end InNode.\n"); //testing to see if this is working, …Run Code Online (Sandbox Code Playgroud)