我如何通过一个数组搜索? [英] How do I search through an array?

查看:137
本文介绍了我如何通过一个数组搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的任务我要创建一个结构,允许用户输入学生信息(ID,出生日期,和放大器;电话号码)。我没有问题,这样做是很简单。现在我需要用学生证可以显示相应的出生日期和电话号码的学生,这是我无法一起工作的问题,通过输入信息进行搜索。如果你看到我的计划有任何其它问题,请让我知道什么是错的,我为什么要改变这样我就可以从我的错误中吸取教训。

我也不能确定如何存储学生信息,以一个数组的所有这些不同的部分并相互对应。所以,当我搜索一个ID,它是如何知道要返回正确的出生日期和电话。我真的在这里失去了球员,并需要一些帮助。什么都告诉我,或者如果你给我code,请解释为什么我应该做的,你告诉我该怎么做。

请注意:我所有的课程都在网上,以便让我的教授的帮助的保持是一个挑战,所以我谈谈你们的帮助。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构infoStruct
{
    INT studentID;
    INT年;
    INT月;
    日整型;
    INT电话;
    INT结束;
};INT主要(无效)
{
    INT学生= 0;
    INT infoArray [学生]
    结构infoStruct信息;
    INT studentID;
    INT年;
    INT月;
    日整型;
    INT电话;
    INT结束;    而(info.end!= -1){
        学生=学生+ 1;
        的printf(请输入学生信息(ID,日,月,年,手机)\\ n);
        的printf(请输入以下-1的电话号码,以结束该进程继续输入0 \\ n);
        scanf函数(%D%D%D,&安培; info.studentID,&安培; info.day,&安培; info.month,&安培; info.year,&安培; info.phone,&安培;信息。结束);
    }
    如果(info.end = -1){
        的printf(您输入%d个学生(S)\\ n,学生);
    }
    //学生搜索
    的printf(请输入你想找的\\ n学生的学生证。);
    scanf函数(%D,info.studentID);
    的printf(DOB:%D%D,电话:%D,info.month,info.day,info.year,info.phone);}


解决方案

好吧,所以你需要使用的学生证,通过infoArray进行搜索。
首先,我想你想你的数组为类型 infoStruct (其中重presents的学生)。 学生是数组中元素的数量。

  //存储的学生信息
的for(int i = 0; I<学生; ++ I)
{
  scanf函数(%D [...],&安培; infoArray [I] .studentID,&安培; infoArray [I] .year [...]);
}

假设你正在搜索其ID,例如,被称为ID1学生。
你会是这样做的:

 的for(int i = 0; I<学生; ++ I)
{
  如果(infoArray [I] .studentID == ID1)
  的printf(%d个,infoArray [I] .phone);
}

我真的不知道我理解你的问题很好,但我希望这有助于。

For my assignment I have to create a structure that allows the user to enter student info (ID, DOB, & Phone number). I have no problem doing this that is quite simple. Now I need to search through that enter info using the student ID to display that students corresponding DOB and phone number, this is the problem that I am having trouble working with. If you see any other problems with my program please let me know what is wrong and why I should change so I can learn from my mistakes.

I'm also not sure how to store all these different parts of the students info to an array and have them correspond to each other. So when I search an ID how does it know to return the correct DOB and phone. I'm really lost here guys and needing some help. What ever you tell me or if you give me code, please explain why I should be doing what you're telling me to do.

NOTE: All my classes are online so getting hold of my professor for help is a challenge, so I've turn to you guys for help.

#include <stdio.h>
#include <stdlib.h>

struct infoStruct 
{
    int studentID;
    int year;
    int month;
    int day;
    int phone;
    int end;
};

int main (void)
{
    int students = 0;   
    int infoArray [students];
    struct infoStruct info;
    int studentID;
    int year;
    int month;
    int day;
    int phone;
    int end;



    while (info.end != -1) {
        students = students + 1;
        printf("Enter student information (ID, day, month, year, phone)\n");
        printf("Enter -1 following the phone number to end the process to continue enter 0\n");
        scanf("%d %d %d %d %d %d", &info.studentID, &info.day, &info.month, &info.year, &info.phone, &info.end);
    }
    if (info.end = -1){
        printf("You entered %d student(s)\n", students);
    }
    //Student Search
    printf("Please enter the student ID of the student your looking for\n.");
    scanf("%d", info.studentID);
    printf(" DOB: %d %d %d, Phone: %d", info.month, info.day, info.year, info.phone);

}

解决方案

Okay, so you need to search through infoArray using the student ID. First of all, I think you want your array to be of type infoStruct (which represents the students). students is the number of elements of the array.

//storing the student info
for (int i=0;i<students;++i)
{
  scanf("%d %d[...]",&infoArray[i].studentID, &infoArray[i].year[...]);
}

Let's say you are searching for the student whose ID is, for example, called id1. You would do that like this:

for (int i=0;i<students;++i)
{
  if (infoArray[i].studentID==id1)
  printf("%d",infoArray[i].phone);
}

I'm not really sure I understood your question well, but I hope this helps.

这篇关于我如何通过一个数组搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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