程序使用for循环打印数组的值

时间:2020-07-01 07:03:55

标签: c

我想使用for循环创建此简单程序。 现在,它应该做的是从数组中获取一个值,并打印出与day(1-30)相对应的值。

这是我的代码。

#include<stdio.h>

int main() {

    int j;
    int days=1;
    int value[]={31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
    
    for(days=1; days<=31; days++){
        
            for(j=value[0]; j<=value[].length; j++){
                printf("%d \t %d\n", days, j);  
            }
        
        
        }
}

当我运行它时,value []不会改变...我相信即时通讯无法增加数组的索引...

有人帮助... 我该怎么办??

3 个答案:

答案 0 :(得分:1)

首先,在C中不允许value[].length]确定数组的长度。另外,在]之后还有一个废弃的length,即使您使用C ++编译器来编译此代码,也会给您带来语法错误。

如果要获取元素数量,则需要使用f.e. sizeof运算符,然后将分配的内存量(以字节为单位)除以每个元素对象的内存:

sizeof(value) / sizeof(value[0])

此外,内部for循环的算法

j = value[0]; j <= value[].length; j++){

没有任何意义。为什么要比较某个数组元素的值与整个数组的长度,而不是遍历数组直到找到匹配的值?

想想你在做什么!

您还需要进行另一次if检查,以证明您遇到了正确的值。

例如:

#include <stdio.h>

int main (void) {

    unsigned int j;
    unsigned int days = 1;
    unsigned int value[] = { 
                             31,30,29,28,27,26,
                             25,24,23,22,21,20,
                             19,18,17,16,15,14,
                             13,12,11,10,9,8,7,
                             6,5,4,3,2,1
                           };

    unsigned int len = sizeof(value) / sizeof(value[0]);

    printf("Day \t Index \t Value\n");
    
    for (days = 1; days <= 31; days++) {
         for (j = 0; j < len ; j++) {
              if (value[j] == days)
                   printf("%u \t %u \t %u\n", days, j, days);  
         }        
    }
}

输出:

Day   Index  Value
1     30     1
2     29     2
3     28     3
4     27     4
5     26     5
6     25     6
7     24     7
8     23     8
9     22     9
10    21     10
11    20     11
12    19     12
13    18     13
14    17     14
15    16     15
16    15     16
17    14     17
18    13     18
19    12     19
20    11     20
21    10     21
22    9      22
23    8      23
24    7      24
25    6      25
26    5      26
27    4      27
28    3      28
29    2      29
30    1      30
31    0      31

注意事项:

  • 我使用了unsigned int,因为通常情况下您的日子是负数,因此最好与返回sizeof的操作进行比较以确定数组{{1 }}。

  • 输出的格式在您的实现上可能会有所不同,因为多少个空格value是特定于实现的。

  • 使用C编译器编译C代码,切勿忽略编译器警告。不要将C与C ++代码混合在一起。

  • 使用\t代替int main(void)。后者不是严格的C标准兼容。

  • 好的免费C入门书是Modern CThe C Programming Language (2nd Edition)。这些以及其他您可以在这里找到:

    The Definitive C Book Guide and List

答案 1 :(得分:0)

好的。我真的还没做你想做的事。
但是,我的理解是,第一天您要打印31 ..
这是我为重构它所做的:

#include<stdio.h>

int main() {

    int j;
    int days=1;
    int value[]={31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
    
    for(days=1; days<=31; days++){
        
            for(j=1; j<=value[days]; j++){
                printf("%d \t %d\n", days, j);
            }
        
        
        }
}

在给定for循环0-31时,很难将一天理解为数字 ...

我希望这对您有所帮助。

答案 2 :(得分:0)

由于您的数据量很大,我建议使用for循环来生成每一天,然后在for循环运行时将它们存储到数组中。您可以在每个循环结束时每天打印。这是您可以不同的方法:

#include<stdio.h>

int main()
{
    int dayCount;
    int days = 31; //if you already knew you want to print days 1 - 31, might as well initiate the array size

    int value[days];
    int arrayCount;

    //assigning days 1 - 31 into the array
    for(dayCount = 1 ; dayCount <= 31 ; dayCount++)
    {
        value[dayCount] = dayCount;
        //you can also choose to print the element of the array here as the loop runs
    }

    //to print all elements of the array
    for(arrayCount = 1 ; arrayCount < days ; arrayCount++)
    {
        printf("Index: %d \tDay: %d\n", arrayCount, value[arrayCount]);
    }
}

但是,仅当您想要僵硬并严格打印第1至31天时,这就足够了。如果您考虑到耐用性,最好查看“ RobertS支持Monica Cellio”的答案。