Tag: for循环

我怎样才能在C中解决这个问题?

我有一个模拟BMI(身体质量指数)计算器的程序,该计算器接收来自10个用户的输入,询问每个用户的重量和高度,然后程序计算BMI并在格式化的输出中打印计算结果。 像这样的东西: USER BMI STATUS 1 : 10 所以我想到了这一点,我会首先接受用户的输入,然后尝试以上面的格式化方式显示输入的计算。 以下是我试图提出的问题,一直向用户询问他们的体重和身高,直到第10位用户。 #include #include int main(void) { float weight_value = 0.0f; float user_height = 0.0f; float BMI = 0.0f; char BMI_Status[30]; int i; for ( i = 1; i <= 10; i++) { printf("Please enter your weight: "); scanf("%f", &weight_value); //Reads in the user's weight printf("Now enter […]

数组中的唯一数字

与我之前的问题类似,这次我试图打印不会出现两次或两次以上的数字。 这是我的代码: #include int main() { int i; int a[10]={2,2,2,4,6,6,9,10,10,11}; for(i=0; i 0 && a[i] != a[i-1]) printf(“%d “,a[i]); } if(i==9 && a[i]!=a[i-1]) printf(“%d”, a[i]); } printf(“\n”); return 0; } 输出结果是正确的,即4,9,11,但我需要分别考虑i的最终值。 有没有更好的办法?

运行时检查失败变量周围的堆栈已损坏

#include main() { int num[9], i = 0, count = 0; while (i<10) { scanf("%d", &num[i]); if (num[i] % 2 == 0) { count++; } i++; } printf("we have %d double numbers\n", count); } 运行时检查失败#2 – 变量周围的堆栈已损坏 我该怎么办?

将for循环中的值存储到数组中

我想将从for-loop读取的值存储到数组中 char A[]; int x; int y=5; for( int i=0; int i =1000; i++) { x = x+y; // then store/append x as elements of the char array, A…. what is the syntax? }

将unsigned char数组的内容分成两半

我想知道是否有人可以帮助我我只学习c,我试图将unsigned char数组的内容分成两半,结果可以存储在两个unsigned int中, 例如,我在下面有一些代码将hex值添加到BYTE数组中,那么如何将val []的内容分成两个但保持相同的顺序 #include typedef unsigned char BYTE; int main() { // Sample purposes putting hex into val[8] int i,j; long long hex=0x78661EB54FE76763; BYTE val[8]; for(j=0,i=7; i>=0; i–,j++){ val[j]= (hex>>(i*8))&0xff; } // How to split the contents of val[8] which now holds the hex return 0; } 我试图将hex值拆分为78661EB5,4FE76763并将每个值存储在unsigned int中,该值存储在我的示例中的val [8]中

for循环的C运行时错误

我写了一小段我认为可以工作的代码,我得到了控制台中显示的所有值,但此后不久我收到了运行时错误。 任何人都知道为什么? #include int array[10]; void main() { int i; for(i = 0; i < 10; i++){ array[i] = i; printf("%i", array[i]); } return; } 输出: Runtime error time: 0 memory: 2248 signal:-1 0123456789 任何帮助将不胜感激,谢谢!

对于循环扫描,c中的时间少一个

用户输入一串字符,但在此之前输入字符串的大小。 然后我必须阅读并计算每个字母输入的次数。 这是我的代码: #include char ab[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’}; //Size = 26 int main(void) { int i, j, size, counter[26]; char temp; for(i=0; i<26; i++) { counter[i]=0; } scanf("%d",&size); for(i=0; i<size; […]

C – 编译器优化如何影响没有主体的for循环?

我有一些遗留代码,其中包含了一个时间浪费循环,以便有时间让eeprom读取完成(不良做法): for(i = 0; i < 50; i++); 但是,当编译器优化开启以获得速度时,会发生特殊情况。 它不一定与该语句相关联,但我想知道编译器是否可能只是优化时间延迟

evp_encrypt不能在c中的for循环中工作

我是新来的,所以请原谅我做错了什么。 我尝试使用加密字符串创建一个数组,我正在使用EVP API进行加密。 这工作正常,但是我尝试在foor循环中使用加密function,控制台什么也没给我。 这是我的加密function: char *encrypt(char *key, char *iv, char * source){ //char *target; int in_len, out_len; EVP_CIPHER_CTX ctx; in_len=strlen((const char *)source); unsigned char *target = (unsigned char *) malloc(in_len); //printf(“This is the text before ciphering: %s\n”,source); //printf(“The length of the string is: %d\n”,in_len); //starting the encryption process EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,(unsigned char*) key,(unsigned char*)iv); EVP_EncryptUpdate(&ctx,target,&out_len,(unsigned […]

在C中的for循环条件中调用strlen()的最佳替代方法是什么?

我已经读过在我的for循环条件中调用strlen()是不好的做法,因为这是一个O(N)操作。 但是,在查看备选方案时,我会看到两种可能的解决方 int len = strlen(somestring); for(int i = 0; i < len; i++) { } 要么… for(int i = 0; somestring[i] != ‘\0’; i++) { } 现在,第二个选项似乎可能具有以下优点:1)不声明不必要的变量,2)如果字符串长度在循环中被修改,只要长度不是<i,它应该仍然到达终点。 但是,我不确定。 其中哪一个是C程序员的标准做法?