【问题标题】:Searching for a value inside a graph and printing the value under it在图表中搜索一个值并打印它下面的值
【发布时间】:2021-02-03 13:12:22
【问题描述】:

我对编码完全陌生,现在正在学习我的第一门 C 语言课程,我们的任务是制作一张芬兰人口在 5 个不同年份的表格(图表)。这我显然可以做到,但问题是用户必须输入一个特定的年份(比如 1950 年),然后程序将搜索表格并告诉那一年的人口。

我的问题是我不知道如何搜索表格。

代码如下:

    int r;
    int s;
    unsigned int pop[2][5] ={1900,1950,2000,2018,2019,2656000,4030000,5181000,5518000,5525000};
    
    for (r=0; r<2;r++)
        {
        for (s=0; s<5;s++)
            printf("%d ",pop[r][s]);
        printf("\n");
}
    printf("\n");
    int year;
    printf("Choose one of the following years: 1900, 1950, 2000, 2018 tai 2019\n");
    scanf("%d", &year);
    
    if (year = pop[r])
        printf("Population fo the year you chose was: %d\n", pop[s]);   

不包括主要等...

谢谢你的帮助!!

【问题讨论】:

  • if (year = pop[r])你确定要做作业吗?
  • 您确定您使用一组基本的编译标志进行编译吗?请参阅编译器的文档,例如对于gcc/clang,例如使用 -Wall -Werror -Wextra -O2 -g 之类的选项。

标签: c


【解决方案1】:

要从表格中搜索,您应该使用循环检查表格的每个元素。

另外请注意,您应该使用== 运算符,而不是= 运算符进行比较。

for (s=0; s<5; s++)
{
    if (year == pos[0][s])
    {
        printf("Population fo the year you chose was: %d\n", pop[1][s]);
    }
}

【讨论】:

  • 非常感谢!这有帮助。它现在可以正常工作,感谢您解释我不明白的内容! :)
猜你喜欢
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多