मैं सी में एक नौसिखिया प्रोग्रामर हूँ और एक मुद्दा लगभग दर्दनाक सरल है कि में चल रहा हूँ। मैं एक बुनियादी कार्यक्रम लिख रहा हूं जो दो एरे बनाता है, छात्र नामों में से एक और छात्र आईडी संख्याओं में से एक बनाता है, फिर उन्हें टाइप करता है और उन्हें विभिन्न तरीकों से प्रिंट करता है, और आखिर में उपयोगकर्ता को आईडी नंबर से सरणी खोजने की अनुमति मिलती है।समस्याएं पैरामीटर के रूप में
#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE 3
#define MAX_NAME_LENGTH 32
int main()
{
// Student info arrays
char NAME[ARRAY_SIZE][MAX_NAME_LENGTH];
int ID[ARRAY_SIZE];
// Array for student IDs, shifted twice to the right
int shiftedID[ARRAY_SIZE];
// Boolean value to keep while loop running and
// the ID search prompt repeating
int loop = 1;
// Counter variable for the for loop
int counter;
// Gets input values for the student info arrays
for (counter = 0; counter < ARRAY_SIZE; counter++)
{
printf("Input student name: ");
scanf("%s", NAME[counter]);
printf("Input student ID: ");
scanf("%d", &ID[counter]);
}
// Sorts the arrays
sort(NAME, ID);
// Prints the arrays
print_array(&NAME, ID);
// Shifts the ID value two bits to the right
shiftright(ID, shiftedID);
print_array(NAME, shiftedID);
// Repeatedely prompts the user for an ID to
// search for
while(loop == 1)
{
search_id(NAME, ID);
}
}
और यहाँ कार्यशील परिभाषाएँ रहे हैं:: यहाँ कोड है
#define ARRAY_SIZE 3
#define MAX_NAME_LENGTH 32
// Sorts the two arrays by student ID. (Bubble sort)
void sort(char **nameArray, int idArray[])
{
// Counter variables for the for loop
int firstCounter = 0;
int secondCounter = 0;
for(firstCounter = 0; firstCounter < ARRAY_SIZE; firstCounter++)
{
for(secondCounter = 0; secondCounter < ARRAY_SIZE - 1;
secondCounter++)
{
if(idArray[secondCounter] > idArray[secondCounter + 1])
{
// Temporary variables for the sort algorithm
int tempInt = 0;
char tempName[32];
tempInt = idArray[secondCounter + 1];
idArray[secondCounter + 1] = idArray[secondCounter];
idArray[secondCounter] = tempInt;
strcpy(tempName, nameArray[secondCounter + 1]);
strcpy(nameArray[secondCounter + 1],
nameArray[secondCounter]);
strcpy(nameArray[secondCounter], tempName);
}
}
}
}
// Searches the ID array for a user input student
// ID and prints the corresponding student's info.
void search_id(char **nameArray, int idArray[])
{
// A boolean value representing whether or not
// the input ID value was found
int isFound = 0;
// The input ID the user is searching for
int searchID = 0;
printf("Input student ID to search for: ");
scanf("%d", &searchID);
// Counter variable for the for loop
int counter = 0;
while (counter < ARRAY_SIZE && isFound == 0)
{
counter++;
if (idArray[counter] == searchID)
{
// Prints the name associated with the input ID
isFound = 1;
printf("%s", nameArray[counter]);
}
}
// If the input ID is not found, prints a failure message.
if (isFound == 0)
{
printf("ID not found.\n");
}
}
// Prints the name and ID of each student.
void print_array(char **nameArray, int idArray[])
{
// Counter variable for the for loop
int counter = 0;
printf("Student Name & Student ID: \n");
for (counter = 0; counter < ARRAY_SIZE; counter++)
{
printf("%s --- %d\n", nameArray[counter], idArray[counter]);
}
}
// Shifts the ID value to the right by two bits
void shiftright(int idArray[], int shiftedID[])
{
// Counter variable for the for loop
int counter = 0;
for (counter = 0; counter < ARRAY_SIZE; counter++)
{
shiftedID[counter] = idArray[counter] >> 2;
}
}
मुझे पता है कि इस कार्यक्रम प्रकृति में काफी बुनियादी है हूँ, और कुछ भी की तुलना में अधिक यह मुझे और अधिक प्राप्त करने के लिए एक व्यायाम है अच्छी तरह से इस तरह के सी मैं कुछ समय के लिए उस पर काम कर रहा है के रूप में एक भाषा में निपुण है, और कई समस्याओं के माध्यम से काम किया है, लेकिन तीन मुद्दों पर अटक कर रहे:
इनपुट आईडी नंबर नहीं कर रहे हैं टी इनपुट पहले से ही क्रम में, एक सेगमेंटेशन गलती परिणाम। यदि आईडी नंबर पहले से ही इनपुट में इनपुट हैं, तो सॉर्ट फ़ंक्शन कभी भी कथन के माध्यम से गुजरता नहीं है, और कोई समस्या नहीं होती है।
print_array फ़ंक्शन में नाम/आईडी के सरणी पास करते समय, आईडी मुद्रित होती हैं, लेकिन नाम पूरी तरह खाली या अजीब पात्रों की एक श्रृंखला के रूप में मुद्रित किए जाएंगे।
जब कार्यक्रम, कि पहले दर्ज किया गया था आईडी नंबर के अंत में आईडी के आधार पर खोज (हां, आईडी में नंबर [0]) एक आईडी नहीं मिला संदेश प्रदर्शित करता है, जहां सूचकांक 1 या अधिक से अधिक पर सभी नंबरों को होगा ठीक काम करें - संबंधित नामों से अलग जिन्हें मुद्रित किया जाना चाहिए, जैसा कि दूसरे अंक में उल्लिखित है।
मुझे जो भी सलाह मिल सकती है, उसकी बहुत सराहना की जाएगी! मुझे सी में आवश्यक बढ़िया विवरणों के पीछे शक्ति मिलती है जो कि वास्तव में दिलचस्प है, लेकिन यह भी बहुत भ्रमित, डरावनी है, और इसका मतलब है कि मुझे जो भी मदद मिल सकती है वह एक बड़ा अंतर बनाती है।
2 डी-ऐरे डबल पॉइंटर ('char **') नहीं है। – BLUEPIXY
पैरामीटर आपके द्वारा पारित सरणी से मेल खाने के लिए 'char nameArray [ARRAY_SIZE] [MAX_NAME_LENGTH]' होना चाहिए। आप 'ARRAY_SIZE' –