我试着做的25 % -9只是为了好玩,我得到的答案是-2(这是在谷歌上)当我用C代码做到这一点时我得到7.有人能解释我为什么有两个不同的答案吗?
为什么以下C代码不输出10
#include<stdio.h>
#include<stdlib.h>
void get_length(char s[]);
int main( )
{
char buff[128];
printf("\nEnter buffer data: ");
scanf("%s",buff);
get_length(buff);
}
void get_length(char *s){
int count;
for(count = 0 ; *s != '\0' ; s++)
count++;
printf("count = %d\n",count);
}
Run Code Online (Sandbox Code Playgroud)
该输入是我罗希特
的输出是
数= 432
谁能请解释.
为什么下面的代码给我一个错误的未定义的sqrt引用.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
double i = 25;
printf("\nSquare root of %d = %d",i,sqrt(i));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经包含了math.h,我正在使用Debian.任何人都可以告诉我出了什么问题.我是否需要再次安装我的库?
我想为加泰罗尼亚语编写代码.加泰罗尼亚语数字定义如下:
C(n) = 2n C n/(n+1).但是,(2n C n)我没有使用计算,而是想使用以下事实计算加泰罗尼亚数字:
Catalan(n) =
2n! /n! * n! * (n+1)
Catalan(n+1) =
2*(n+1)
--------------------------- =
(n+1)! * (n+1)! * ((n+1)+1)
(2n+2) * (2n+1) * 2n!
------------------------------- =
(n+1) * n! * (n+1) * n! * (n+2)
(2n+2) * (2n+1) * 2n!
----------------------------------- =
(n+1) * (n+2) * n! * n! * (n+1)
(2n+2) * (2n+1)
--------------- * Catalan(n)
(n+1) * (n+2)
Run Code Online (Sandbox Code Playgroud)
现在利用上述事实,这是我的以下代码:
int catalan(int n)
{
if …Run Code Online (Sandbox Code Playgroud) 我在C和C++中尝试了以下代码.file1是ac文件.file2是一个c ++文件,file3是名称magling的头文件.
在file1.c
#include<stdio.h>
#include<stdlib.h>
#include "file3.hpp"
int main(int argc,char **argv)
{
int a[5];
int i;
for(i=0;i<5;i++)
a[i] = i;
printf("%d",a[17]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
file2.cpp
#include "file3.hpp"
int printtrial(int number)
{
return number;
}
Run Code Online (Sandbox Code Playgroud)
file3.hpp
#ifdef __cplusplus
extern "C"
{
#endif
extern int printtrial(int number);
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
我使用以下命令编译它:
gcc -c file1.c
g++ -c file2.cpp
gcc -o output file1.o file2.o
Run Code Online (Sandbox Code Playgroud)
在此,它给出了错误:
file2.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
谁能告诉我发生了什么事!
在下面的代码中,我将二进制转换为十进制,然后打印与之对应的字符。
void convertToChar(int binaryChar[],int length)
{
int multiplier = 0;
int i;
int sum = 0;
for(i=length;i>=0;i++)
{
sum = sum + (binaryChar[i]*pow(2,multiplier));
multiplier = multiplier + 1;
}
printf("\nThe character is: %c",sum);
}
Run Code Online (Sandbox Code Playgroud)
问题出在这一行。sum = sum + (binaryChar[i]*pow(2,multiplier));它抛出错误: warning: converting toint' from double'。请帮忙!
我写了下面的代码,用牛顿的方法通过连续的近似来找到平方根,但它没有给我正确的答案.有人可以解释一下吗?
#include<stdio.h>
#include<stdlib.h>
#define square(x) x*x
double rootByNewtonApprox(int n);
double improve(double n);
double average(double a,double b);
int goodEnough(double guess);
double guess(int n);
int number;
int main(void)
{
double root;
printf("\nEnter the number you want square root of: ");
scanf("%d",&number);
if(number<0)
number = -1* number;
root = rootByNewtonApprox(number);
printf("\nThe square root of %d is %lf\n",number,root);
return 0;
}
double guess(int n)
{
return n/2;
}
double rootByNewtonApprox(int n)
{
if(goodEnough(guess(n)))
return guess(n);
else
rootByNewtonApprox(improve(guess(n)));
}
double improve(double guess)
{
return average(guess,(number/guess)); …Run Code Online (Sandbox Code Playgroud) 我使用C中的指针编写了以下代码用于字符串连接
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void strCat(char *str1,char *str2);
int main(void)
{
char str1[] = "Rohit";
char str2[] = "Kumar";
strCat(str1,str2);
return 0;
}
void strCat(char *str1,char *str2)
{
int i;
char *start;
start = str1;
printf("%s",str1);
while(*str1++ != '\0')
continue;
while(*str2 != '\0')
*str1++ = *str2++;
*str1 = '\0';
printf("%s\n",str1);
}
Run Code Online (Sandbox Code Playgroud)
为什么输出是Rohit(null).请帮忙!!
我在Python中有以下列表.
[[53.60495722746216, 'Percent Cats'],
[45.298311033121294, 'Percent Dogs'],
[1.0967317394165388, 'Percent Horses']]
Run Code Online (Sandbox Code Playgroud)
现在我想要动物的百分比最高.在这种情况下,它会Cats.
如何对此结构进行排序以获取值?
我写了memoized一个用C计算数字因子的代码.但是在提供输入时n=3它会将输出视为6! = 134513904.可以请某人解释出现了什么问题?
int fact(int n)
{
int temp;
static int lookup_table[100];
if(lookup_table[n])
return lookup_table[n];
else if(n == 0 )
{
lookup_table[0]= 1;
return 1;
}
else
{
temp = n * fact(n-1);
lookup_table[n] = temp;
return temp;
}
}
Run Code Online (Sandbox Code Playgroud)