C程序:为什么这个数组有这样的输出? [英] C Program: Why does this array have this output?

查看:128
本文介绍了C程序:为什么这个数组有这样的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我从来没有采取C编程过程中,我有什么数组是一个总的想法,但我似乎无法找出为什么这个数组有一定的输出。

 的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&stdlib.h中GT; //包括srand函数
#pragma警告(禁用:4996)
#定义尺寸15
主(){
    int类型的【尺寸】;
    INT I;
    双B1【尺寸】,B2【尺寸】;
    函数srand((无符号)时间(NULL)); //使用当前时间为种子
    //申请(开始)时间戳
       对于(i = 0; I<尺寸;我++)
    {
        一个由[i] =兰特()%100;
              //用0到99之间的随机数初始化数组
        B1 [I] = A [I] / 5;
        的printf(一[%D]。=%d个\\ N,我,一个[I]);
        的printf(B1 [%d个=%F \\ N,我,B1 [I]);
        B2 [I] = a [i];
        B2 [I] = B2 [I] / 5;
        的printf(B2 [%d个=%F \\ N,我,B2 [I]);
    }
    //申请(完)时间戳
    //计算在几秒钟内经过的时间和显示时间
}

有关我的功课,我需要运行这个code,并解释为什么B1和B2具有相同或不同的值。我似乎无法环绕正在发生的事情在这里我的头:

 为(i = 0; I<尺寸;我++)
    {
        一个由[i] =兰特()%100;
              //用0到99之间的随机数初始化数组
        B1 [I] = A [I] / 5;
        的printf(一[%D]。=%d个\\ N,我,一个[I]);
        的printf(B1 [%d个=%F \\ N,我,B1 [I]);
        B2 [I] = a [i];
        B2 [I] = B2 [I] / 5;
        的printf(B2 [%d个=%F \\ N,我,B2 [I]);
    }

如果有人能说明什么code为这里做什么我就非常AP preciate它。
谢谢!

编辑 - 这是输出一旦我运行程序:

  A [0] = 8
B1 [0] = 1.000000
B2 [0] = 1.600000
一个[1] = 74
B1 [1] = 14.000000
B2 [1] = 14.800000
一个[2] = 78
B1 [2] = 15.000000
B2 [2] = 15.600000
一个[3] = 64
B1 [3] = 12.000000
B2 [3] = 12.800000
一个[4] = 53
B1 [4] = 10.000000
B2 [4] = 10.600000
一个[5] = 6
B1 [5] = 1.000000
B2 [5] = 1.200000
一个[6] = 71
B1 [6] = 14.000000
B2 [6] = 14.200000
一个[7] = 4
B1 [7] = 0.000000
B2 [7] = 0.800000
一个[8] = 7
B1 [8] = 1.000000
B2 [8] = 1.400000
一个[9] = 57
B1 [9] = 11.000000
B2 [9] = 11.400000
一个[10] = 55
B1 [10] = 11.000000
B2 [10] = 11.000000
一个[11] = 13
B1 [11] = 2.000000
B2 [11] = 2.600000
一个[12] = 55
B1 [12] = 11.000000
B2 [12] = 11.000000
一个[13] = 96
B1 [13] = 19.000000
B2 [13] = 19.200000
一个[14] = 25
B1 [14] = 5.000000
B2 [14] = 5.000000


解决方案

由于您隐式转换 INT 双击并且这两个时间做不同的方式。

  B1 [I] = A [I] / 5;
/ * ^^ INT ^
 * | + - INT
 * + - 隐皈依翻番* /B2 [I] = B2 [I] / 5;
/ * ^ ^双
 * + INT隐式转换
 *翻番* /


  • 在第一八佰伴pression你把 INT INT 你失去precision(即7月2日给你3,没有3.5),

  • 在第二个前pression你把双击双击。这不会导致这一点。

Ok so I have never taken a C programming course and I have a general idea of what Arrays are, but I cant seem to figure out why this array has a certain output.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>  // include srand function
#pragma warning(disable : 4996) 
#define Size 15
main() {
    int a[Size];
    int i;
    double b1[Size], b2[Size];
    srand( (unsigned)time( NULL ) );  // Use current time as seed
    // apply (start) timestamp 
       for (i = 0; i < Size; i++)
    {
        a[i] = rand() % 100; 
              // initialize the array using random number between 0 and 99
        b1[i] = a[i]/5;
        printf("a[%d] = %d\n", i, a[i]);
        printf("b1[%d] = %f\n", i, b1[i]);
        b2[i] = a[i];
        b2[i] = b2[i]/5;
        printf("b2[%d] = %f\n", i, b2[i]);
    }
    // apply (end) timestamp 
    //compute the time elapsed and display time in seconds
}

For my homework I need to run this code and explain why b1 and b2 have same or different values. I cant seem to wrap my head around what is happening here:

       for (i = 0; i < Size; i++)
    {
        a[i] = rand() % 100; 
              // initialize the array using random number between 0 and 99
        b1[i] = a[i]/5;
        printf("a[%d] = %d\n", i, a[i]);
        printf("b1[%d] = %f\n", i, b1[i]);
        b2[i] = a[i];
        b2[i] = b2[i]/5;
        printf("b2[%d] = %f\n", i, b2[i]);
    }

If someone could explain what the code is doing here I would very much appreciate it. Thanks!

Edit- This is the output once I run the program:

a[0] = 8
b1[0] = 1.000000
b2[0] = 1.600000
a[1] = 74
b1[1] = 14.000000
b2[1] = 14.800000
a[2] = 78
b1[2] = 15.000000
b2[2] = 15.600000
a[3] = 64
b1[3] = 12.000000
b2[3] = 12.800000
a[4] = 53
b1[4] = 10.000000
b2[4] = 10.600000
a[5] = 6
b1[5] = 1.000000
b2[5] = 1.200000
a[6] = 71
b1[6] = 14.000000
b2[6] = 14.200000
a[7] = 4
b1[7] = 0.000000
b2[7] = 0.800000
a[8] = 7
b1[8] = 1.000000
b2[8] = 1.400000
a[9] = 57
b1[9] = 11.000000
b2[9] = 11.400000
a[10] = 55
b1[10] = 11.000000
b2[10] = 11.000000
a[11] = 13
b1[11] = 2.000000
b2[11] = 2.600000
a[12] = 55
b1[12] = 11.000000
b2[12] = 11.000000
a[13] = 96
b1[13] = 19.000000
b2[13] = 19.200000
a[14] = 25
b1[14] = 5.000000
b2[14] = 5.000000

解决方案

Because you implicitly convert int to double and both times do that different ways.

b1[i] =   a[i]   / 5   ;
/*     ^  ^ int    ^ 
 *     |           +-- int
 *     +-- implicit convertion to double */

b2[i] =  b2[i]     /  5   ;
/*        ^ double    ^
 *                    + int implicitly converted
 *                      to double */

  • In first expression you divide int by int where you lose precision (i.e. 7 / 2 gives you 3, not 3.5),
  • In second expression you divide double by double. That doesn't lead to that.

这篇关于C程序:为什么这个数组有这样的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆