知道为什么C打印随机数? [英] Any idea why C is printing random numbers?

查看:87
本文介绍了知道为什么C打印随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我花了几个小时试图解决这个问题。我测试了它没有一个数组和循环,写的答案打印为单个值,但当它放在一个数组时,它似乎把它搞砸了?

我刚开始学习C所以任何提示或帮助非常感谢!

谢谢



Hi I spent a couple of hours trying to figure this out. I tested it without a an array and loop and the write answer was printed for a single value but when its placed in an array it just seems to screw it all up?
I have just started learning C so any tips or help is very much appreciated!
Thanks

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
    float array[18][3];
    float Sin_Value[18], Cos_Value, Tan_Value;
    int Degrees = 0;
    float radians[18];
    int i,j;
    float PI=3.14159;

    for(j=0, Degrees=0;j<=17 && Degrees <= 180; ++j, Degrees= Degrees+10)
    {
            radians[j]= Degrees*PI/180;
            Sin_Value[j]=sin(radians[j]);
            Sin_Value[j]= array[j][1];
            printf("%.5f\n %.5f\n %.5f\n", radians[j], Sin_Value[j], array[j][0]);
    }
    getch();
}

推荐答案

您还没有说过您希望实现的目标,因此很难回答。



1.简化循环。

2.你希望sin值在数组中在哪里?你想要打印出什么?您正在打印从未获得值的数组[j] [0]。事实上,阵列中根本没有任何内容。



尝试这样的事情并继续工作:



You have not said what you are wishing to achieve so answering is difficult.

1. Simplify the loop.
2. Where do you want sin value to be in the array and what do you want to print out? You are printing array[j][0] which never gets a value. In fact nothing is put into the array at all.

Try something like this and work on it:

for(j=0; j < 18 ; j++)
{
        Degrees = j * 10;

        radians[j]= Degrees*PI/180;
        Sin_Value[j]=sin(radians[j]);
        array[j][1] = Sin_Value[j];
        printf("%.5f\n %.5f\n %.5f\n", radians[j], Sin_Value[j], array[j][1]);
}
getch();


array [j] [1]



你永远不会初始化数组



你设置 Sin_Value [j] 两次。



我认为不需要数组,但是如果你想存储东西你可以声明一个 struct ,包含度,弧度,sin,cos,tan等,并制作一个数组。这可能是一个很好的练习。
array[j][1]

You never initialize array.

And you set Sin_Value[j] twice.

I don't see any need for arrays, but if you want to store stuff you could declare a struct that contains degrees, radians, sin, cos, tan, etc. and make an array of those. It might be a good exercise.


这篇关于知道为什么C打印随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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