C程序:打破一个int到int数组[] [英] C program: Breaking an int into an int array[]

查看:131
本文介绍了C程序:打破一个int到int数组[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我工作的一个泽勒的规则C计划,试图打破一个4位数的int值重新presents特定日期的年份到含有前2位和最后2位数字表示的2个不同的INT变量一年。

So I'm working on a Zeller's Rule c program, trying to break a 4 digit int that represents the year of a specific date into 2 different int variables containing the first 2 digits and last 2 digits of said year.

void main()
{
    int day, month, year;
    .
    .
    .
    printf("Enter a Year: ");
    scanf("%i",&year);
    .
    .
    .
    int data, i;    
    int split[3];
    for(i=3 ; i>=0 ; i--)           //Problem is in the loop
    {
        data = year % 10;
        split[i] = data;
        year /= 10; 
        printf("Data%i: %i, should be %i\n", i, split[i], data);
    }
}

以上code输出:(INT年= 1234)

The above code outputs: (int year = 1234)

Data3: 0, should be 4
Data2: 0, should be 0
Data1: 0, should be 0
Data0: 0, should be 0

不过,如果我修改打成上面问题的循环:

However, if i modify the loop labeled as the problem above as:

int data, i;    
int split[3];
for(i=3 ; i>=0 ; i--)      //Data is all there, correctly
{                          //Problem arises when I try to store to my array
    data = year % 10;
    year /= 10; 
    printf("Data%i: should be %i\n", i, data);
}

以上code输出的变化:(INT年= 1234)

output of above code changes: (int year = 1234)

Data3: should be 4
Data2: should be 3
Data1: should be 2
Data0: should be 1

我完全失去了,为什么程序0的,当我尝试了一切,把那有没有到我的阵列中的数据。一直让我头疼了好几个小时,因为我显然不知道是什么问题。

I am completely lost as to why the program 0's out everything as soon as I try to put the data that IS THERE into my array. Been giving me a headache for hours as I obviously have no idea what the problem is.

推荐答案

您在你吐阵列3细胞(内部分裂[3] ),但您分配4时间到它:

You have 3 cells in your spit array (int split[3]), but you assign 4 times into it:

for(i=3 ; i>=0 ; i--)

将其更改为内部分裂[4]

这篇关于C程序:打破一个int到int数组[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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