2016-04-22 11 views
5

मैं सी में एक नौसिखिया प्रोग्रामर हूँ और एक मुद्दा लगभग दर्दनाक सरल है कि में चल रहा हूँ। मैं एक बुनियादी कार्यक्रम लिख रहा हूं जो दो एरे बनाता है, छात्र नामों में से एक और छात्र आईडी संख्याओं में से एक बनाता है, फिर उन्हें टाइप करता है और उन्हें विभिन्न तरीकों से प्रिंट करता है, और आखिर में उपयोगकर्ता को आईडी नंबर से सरणी खोजने की अनुमति मिलती है।समस्याएं पैरामीटर के रूप में

#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; 
    } 
} 

मुझे पता है कि इस कार्यक्रम प्रकृति में काफी बुनियादी है हूँ, और कुछ भी की तुलना में अधिक यह मुझे और अधिक प्राप्त करने के लिए एक व्यायाम है अच्छी तरह से इस तरह के सी मैं कुछ समय के लिए उस पर काम कर रहा है के रूप में एक भाषा में निपुण है, और कई समस्याओं के माध्यम से काम किया है, लेकिन तीन मुद्दों पर अटक कर रहे:

  1. इनपुट आईडी नंबर नहीं कर रहे हैं टी इनपुट पहले से ही क्रम में, एक सेगमेंटेशन गलती परिणाम। यदि आईडी नंबर पहले से ही इनपुट में इनपुट हैं, तो सॉर्ट फ़ंक्शन कभी भी कथन के माध्यम से गुजरता नहीं है, और कोई समस्या नहीं होती है।

  2. print_array फ़ंक्शन में नाम/आईडी के सरणी पास करते समय, आईडी मुद्रित होती हैं, लेकिन नाम पूरी तरह खाली या अजीब पात्रों की एक श्रृंखला के रूप में मुद्रित किए जाएंगे।

  3. जब कार्यक्रम, कि पहले दर्ज किया गया था आईडी नंबर के अंत में आईडी के आधार पर खोज (हां, आईडी में नंबर [0]) एक आईडी नहीं मिला संदेश प्रदर्शित करता है, जहां सूचकांक 1 या अधिक से अधिक पर सभी नंबरों को होगा ठीक काम करें - संबंधित नामों से अलग जिन्हें मुद्रित किया जाना चाहिए, जैसा कि दूसरे अंक में उल्लिखित है।

मुझे जो भी सलाह मिल सकती है, उसकी बहुत सराहना की जाएगी! मुझे सी में आवश्यक बढ़िया विवरणों के पीछे शक्ति मिलती है जो कि वास्तव में दिलचस्प है, लेकिन यह भी बहुत भ्रमित, डरावनी है, और इसका मतलब है कि मुझे जो भी मदद मिल सकती है वह एक बड़ा अंतर बनाती है।

+0

2 डी-ऐरे डबल पॉइंटर ('char **') नहीं है। – BLUEPIXY

+0

पैरामीटर आपके द्वारा पारित सरणी से मेल खाने के लिए 'char nameArray [ARRAY_SIZE] [MAX_NAME_LENGTH]' होना चाहिए। आप 'ARRAY_SIZE' –

उत्तर

5
समस्या

है कि आप मानते हैं कि char [ARRAY_SIZE][MAX_NAME_LENGTH] और char ** परस्पर विनिमय कर रहे

void sort(char **nameArray, int idArray[]) 

void sort(char nameArray[][MAX_NAME_LENGTH], int idArray[]) 

या

void sort(char (*nameArray)[MAX_NAME_LENGTH], int idArray[]) 

होना चाहिए एक सरणी के लिए एक सूचक का उपयोग करने के क्रम में है MAX_NAME_LENGTHchar एस, आपके search_id फ़ंक्शन के लिए समान है।

मैं अपने कार्यक्रम के पुनर्गठन के आप सलाह देंगे question 6.13 of C-FAQ

+1

को छोड़ सकते हैं यह मेरी सभी समस्याओं को तुरंत हल कर देता है। तत्पर जवाब के लिए धन्यवाद। – Geoiv04

2

को एक नजर डालें।

typedef struct student 
{ 
    int id; 
    char name[MAX_NAME_LENGTH]; 
} student_t; 

student_t students[ARRAY_SIZE]; 

अब आप एक सारिणी का है जो कभी नहीं बन सकता है "बेमेल" बिना आईडी छँटाई के द्वारा है: बल्कि नाम और आईडी के लिए दो स्वतंत्र सरणियों भंडारण के बजाय, आप structs जो सभी आवश्यक डेटा होते हैं में से एक सरणी स्टोर कर सकते हैं नाम, इत्यादि

आप मानक पुस्तकालय समारोह qsort() का उपयोग कर सी में एक सरणी सॉर्ट कर सकते हैं:

qsort(students, ARRAY_SIZE, sizeof(student_t), comparator); 

इसके लिए आवश्यक है कि आप एक तुलनित्र है, जो काफी सरल है परिभाषित करते हैं। एक उदाहरण होगा:

int comparator(const void *lhs, const void *rhs) 
{ 
    const student_t *s1 = lhs, *s2 = rhs; 
    return s1->id - s2->id; 
} 

आप के बाद यह सॉर्ट हो जाता है छात्रों की सरणी खोज करने के लिए एक और मानक पुस्तकालय समारोह bsearch() के साथ एक ही तुलनित्र का उपयोग कर सकते हैं:

student_t key = { 42 }; // name doesn't matter, search by ID 
student_t* result = bsearch(&key, students, ARRAY_SIZE, sizeof(student_t), comparator); 

ये मानक कार्यों से अधिक प्रभावी हैं क्या आपके पास गलतियों के लिए कम संभावनाओं के साथ, आपको बहुत कम कोड लिखने की आवश्यकता थी।

+0

यह प्रोग्राम लिखने का एक और अधिक प्रभावी तरीका प्रतीत होता है, इसलिए मैं भविष्य में ऐसा कुछ उपयोग करने की संभावना रखूंगा। उत्कृष्ट जवाब किसी भी तरह से। – Geoiv04

संबंधित मुद्दे