我是否需要为我的作业三个独立的数组? [英] Do I need to create three separate arrays for my assignment?
问题描述
好吧首先我会解释我的任务。对于这个任务,我不得不使用它我有没有问题,动态内存分配。什么我有是搞清楚工作我分配正确的方式有问题。对于我的任务,我需要建立一个程序,提示用户输入他们有多少学生然后问以下信息;学号,生日和电话号码。我需要使用一个循环来提示用户输入所有学生的信息。我需要创建一个循环,就能把所有的学生ID扫描并发现使用他们的生日最老的学生(循环必须通过3个以上的学生能够扫描)。
下面是我的code,我还没有做过这还多,因为我不知道真正从哪里开始。我已经设置了动态内存分配,但我不知道如何工作的这个休息。请帮我。
感谢您。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{
为int * studentData = NULL;INT学生;
的printf(有多少学生进入你的记录:\\ n);
scanf函数(%d个,&安培;生);studentData =(INT *)malloc的((的sizeof(int)的*学生));
}
您可以定义一个结构:
//定义一个类型,如int,焦炭,双...
typedef结构studentDataType {
INT ID;
INT birthDateDay;
INT birthDateMonth;
INT birthDateYear;
INT phoneNumber的;
};
然后创建一个数组,其中各个元素的类型是studentData的:
//创建一个数组,其中每个元素的类型是studentData的
studentDataType * studentData =(studentDataType *)malloc的(numberOfStudents *的sizeof(studentData));
然后通过他们循环使用:
的for(int i = 0; I< numberOfStudents ++我){
的printf(%I%I%I \\ N,studentData [I] .ID,studentData [I] .phoneNumber);
}
Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to create a loop that will scan through all the student IDs and find the oldest student using their birthdate (The loop must be able scan through more then 3 students).
Here is my code, I havent done much in it yet because I'm not sure really where to start. I've already setup the dynamic memory allocation, but I don't know how to work the rest of this. Please help me.
Thank you.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int * studentData= NULL;
int students;
printf("How many students are you entering records for:\n");
scanf("%d", &students);
studentData=(int*)malloc((sizeof(int)*students));
}
You could define a structure:
//Define a type, such as int, char, double...
typedef struct studentDataType {
int ID;
int birthDateDay;
int birthDateMonth;
int birthDateYear;
int phoneNumber;
};
Then create an array, where each of those elements is of type studentData:
//Create an array, where each element is of type studentData
studentDataType *studentData = (studentDataType *)malloc(numberOfStudents * sizeof(studentData));
Then loop through them with:
for (int i = 0 ; i < numberOfStudents ; ++i) {
printf("%i %i %i\n", studentData[i].ID, studentData[i].phoneNumber);
}
这篇关于我是否需要为我的作业三个独立的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!