nabz0r
Μηχανικών Πληροφορικής ΤΕ
Μηνύματα: 624
Θετικοί ψήφοι: +75
Αποσυνδεδεμένος
(•‿•)
|
07 Δεκεμβρίου 2009, 23:01
|
0
|
Η εκφώνηση
1.Θα ορίζει μια δομή customer με μέλη τον κωδικό και τον μισθό τ 2.Θα διαβάζει τον αριθμό 3.Θα δεσμεύει μνήμη για έναν πίνακα με στοιχεία τύπου customer του οποίου το μέγεθος θα πρέπει να αποδεσμεύεται στο τέλος της εκτέλεσης του προγράμματος. 4.Θα διαβάζει τα στοιχεία των υπαλλήλων 5.Θα εμφανίζει το ΜΟ των μισθών τους
Συναρτήσεις που πρέπει να χρησιμοποιήσουμε : void read_num(int *num) -- για να διαβάζει τον αριθμό των υπαλλήλων struct customer *allocate(int count -- θα δεσμεύει μνήμη για έναν πίνακα με στοιχεία τύπου customer του οποίου το μέγεθος θα είναι ο αριθμός των υπαλλήλων void read_data (struct customer *cust_arr,int count) --για διάβασμα void show_data (struct customer *cust_arr,int count) -- για εμφάνιση
Το πρόβλημά μου είναι στο 3ο υποερώτημα. Έχω σπάσει το καφάλι μου αλλά δν μου βγαίνει. αυτά έχω κάνει μέχρι τώρα
#include <stdio.h> #include <stdlib.h> #include <conio.h>
struct customer{ int code; float money; }; typedef struct customer pel;
void read_num(int *num); pel *allocate(int count);
void main() { int atoma; pel p; read_num(&atoma); p=allocate(atoma); //apo edw arxizei to problima.oute gia thn dilwsh tou 'p' eimai sigouros
getch(); }
void read_num(int *num) { printf ("Dwse ton ari8mo twn ipallilwn :"); scanf("%d",num); }
pel *allocate(int count) //kai edw synexizei { struct customer d; d=(pel)malloc(sizeof(pel) * count); return d; }
το υπόλοιπο μέρος του κώδικα είναι σωστό (και τα αλλα 2 υποερωτήματα είναι εύκολα) αλλά αυτό το 3ο με σκοτώνει. Αν μπορείτε να βοηθήσετε γιατί μου έχει ανέβει η πίεση 2 μέρες τώρα. ΥΓ : είναι άσκηση που κάναμε στο εργαστήριο. Ισως κάποιοι την θυμούνται
|
|
|
Καταγράφηκε
|
♫ You're older than you've ever been, and now you're even older ...and now you're even older...and now you're even older... ♪
|
|
|
|
fss
Μηχανικών Πληροφορικής ΤΕ
Μηνύματα: 207
Θετικοί ψήφοι: +5
Αποσυνδεδεμένος
|
Πρέπει να δηλώσεις το p και το d ως δείκτες. Η αποδέσμευση της μνήμης γίνεται με τη συνάρτηση free( ) .
Kάπως έτσι:
#include <stdio.h> #include <stdlib.h> #include <conio.h>
struct customer{ int code; float money; }; typedef struct customer pel;
void read_num(int *num); pel *allocate(int count);
void main() { int atoma; pel *p; read_num(&atoma); p=allocate(atoma); //apo edw arxizei to problima.oute gia thn dilwsh tou 'p' eimai sigouros free(p); getch(); }
void read_num(int *num) { printf ("Dwse ton ari8mo twn ipallilwn :"); scanf("%d",num); }
pel *allocate(int count) //kai edw synexizei { struct customer *d; d=malloc(sizeof(pel) * count); return d; }
|
|
|
Καταγράφηκε
|
|
|
|
nabz0r
Μηχανικών Πληροφορικής ΤΕ
Μηνύματα: 624
Θετικοί ψήφοι: +75
Αποσυνδεδεμένος
(•‿•)
|
Thnx man Επίσης δν χρειαζόταν το (pel) πριν την malloc.
|
|
|
Καταγράφηκε
|
♫ You're older than you've ever been, and now you're even older ...and now you're even older...and now you're even older... ♪
|
|
|
echo
Μηνύματα: 34
Θετικοί ψήφοι: +3
Αποσυνδεδεμένος
PhD
|
Δεν πρέπει να δηλώσεις τις συναρτήσεις όλες πριν τη main? Έτσι?? void read_num (int *num); struct customer *allocate (int count); void read_data (struct customer *cust_arr, int count); void show_data (struct customer *cust_arr, int count); edit: Έκανα το πιο κάτω και μου βγάζει πολλά λάθη που δεν μπορώ να καταλάβω. Αν υπάρχει κανένας που έχει όρεξη για αποσφαλμάτωση ας απαντήσει. #include <stdio.h> #include <conio.h> #include <stdlib.h>
struct customer { int code; float sulary; };
typedef struct customer pel;
void read_num (int *num); pel *allocate (int count); void read_data (pel *p, int count); void show_data (pel *p, int count);
void main () { int arithmos; pel p; float avg,sum=0; read_num(&arithmos); p=allocate(arithmos); read_data(p); show_data(p); free (p); getch();
}
void read_num (int *num) { printf("Dose ton arithmo ton ipalilon\n"); scanf("%d" &num); }
pel *allocate (int count) { pel *d; d=malloc(sizeof(pel) * count); return d; }
void read_data (pel p[]) { int i; for (i = 0; i <arithmos; i++) { printf("\n PELATES \n"); printf(" Give code \n"); scanf("%d", p[i].code); printf(" Give sulary \n"); scanf("%f", p[i].sulary); sum = sum + p[i].sulary;
} }
void show_data (pel p[], int count); { int i; for (i = 0; i < aritmos; i++) { printf(" %d \n " ,p[i].code); printf("%f \n",p[i].sulary); printf ("\n \n \n"); avg=sum/arithmos; printf("The average is : %2.f\n ", avg) } }
|
|
Τελευταία τροποποίηση: 15 Ιανουαρίου 2010, 23:15 από echo
|
Καταγράφηκε
|
|
|
|
sarig
|
#include <stdio.h> #include <conio.h> #include <stdlib.h>
struct customer { int code; float sulary; };
typedef struct customer pel;
void read_num (int *num); pel *allocate (int count); void read_data (pel *p, int count); void show_data (pel *p, int count);
void main () { int arithmos; pel p; float avg,sum=0; read_num(&arithmos); p=allocate(arithmos); //το p δεν είναι δείκτης. read_data(p); //έχεις δηλώσει ότι θα δίνεις 2 ορίσματα και δίνεις 1. show_data(p); // το ίδιο και εδώ. free (p); //είπαμε το p δεν είναι δείκτης. getch();
}
void read_num (int *num) { printf("Dose ton arithmo ton ipalilon\n"); scanf("%d" &num); //ξέχασες το κόμμα. }
pel *allocate (int count) { pel *d; d=malloc(sizeof(pel) * count); return d; }
void read_data (pel p[]) //ξανά, πάνω δήλωσες 2, εδώ περνάς 1, και δεν νομίζω να το περνάς και σωστά { int i; for (i = 0; i <arithmos; i++) //δεν έχεις δηλώσει το arithmos { printf("\n PELATES \n"); printf(" Give code \n"); scanf("%d", p[i].code); printf(" Give sulary \n"); scanf("%f", p[i].sulary); sum = sum + p[i].sulary; //δεν έχεις δηλώσεις το sum
} }
void show_data (pel p[], int count); //δεν χρειάζεται το ερωτηματικό { int i; for (i = 0; i < aritmos; i++) { printf(" %d \n " ,p[i].code); printf("%f \n",p[i].sulary); printf ("\n \n \n"); avg=sum/arithmos; //επίσης δεν έχεις δηλώσει τίποτα από αυτά. printf("The average is : %2.f\n ", avg) } }
^^ έγραψα σε σχόλιο κάποια κραυγαλέα λάθη, αν εμφανιστούν άλλα βλέπουμε.
|
|
|
Καταγράφηκε
|
|
|
|
echo
Μηνύματα: 34
Θετικοί ψήφοι: +3
Αποσυνδεδεμένος
PhD
|
#include <stdio.h> #include <conio.h> #include <stdlib.h>
struct customer { int code; float sulary; };
typedef struct customer pel;
void read_num (int *num); pel *allocate (int count); void read_data (pel *p, int count); void show_data (pel *p, int count);
void main () { int arithmos; pel *p; float avg,sum=0; read_num(&arithmos); p=allocate(arithmos); read_data(p,arithmos); show_data(p,arithmos); free (p); getch();
}
void read_num (int *num) { printf("Give number of customers\n"); scanf("%d",&num); }
pel *allocate (int count) { pel *d; d=malloc(sizeof(pel) * count); return d; }
void read_data (pel p[],int arithmos) { int i; for (i = 0; i <arithmos; i++) { printf("\n CUSTOMERS \n"); printf(" Give code \n"); scanf("%d", p[i].code); printf(" Give sulary \n"); scanf("%f", p[i].sulary);
} }
void show_data (pel p[], int arithmos) { int i; float sum,avg; for (i = 0; i < arithmos; i++) { printf(" %d \n " ,p[i].code); printf("%f \n",p[i].sulary); printf ("\n \n \n"); sum = sum + p[i].sulary; avg = sum/arithmos; printf("The average is : %2.f\n ", avg) ; } }
|
|
Τελευταία τροποποίηση: 16 Ιανουαρίου 2010, 01:16 από echo
|
Καταγράφηκε
|
|
|
|
lafs
Μηχανικών Πληροφορικής ΤΕ
Μηνύματα: 839
Θετικοί ψήφοι: 0
Αποσυνδεδεμένος
|
Σας παραθέτω το πρόγραμμα λειτουργικό σύμφωνα με το πρότυπο ANSI:
#include <stdio.h> #include <stdlib.h>
typedef unsigned int Uint;
typedef struct { Uint Code; float Salary; } CustomerT;
CustomerT * Allocate (Uint N); void InputData (CustomerT * p, Uint N); void OutputData (CustomerT * p, Uint N); Uint getNumber (void);
int main (Uint argc, char *argv[]) { CustomerT * p; float avg; Uint N;
printf("Number Of Customers: "); N = getNumber();
p = Allocate(N);
InputData(p, N); OutputData(p, N);
free(p); return EXIT_SUCCESS; }
Uint getNumber (void) { Uint n; scanf("%u", &n); return n; }
CustomerT * Allocate (Uint N) { return (CustomerT *) malloc(sizeof(CustomerT) * N); }
void InputData (CustomerT * p, Uint N) { Uint i;
printf("\nInput Data.\n\n"); for (i = 0; i < N; i++) { printf("\tCustomer: %u.\n", i+1);
printf("\t\tCode: "); scanf("%u", &p[i].Code);
printf("\t\tSalary: "); scanf("%f", &p[i].Salary); } }
void OutputData (CustomerT * p, Uint N) { Uint i; float sum, avg; sum = avg = 0;
printf("\nOutput Data.\n\n"); for (i = 0; i < N; i++) { printf("\tCustomer: %u.\n", i+1);
printf("\t\tCode: "); printf("%u.\n", p[i].Code); printf("\t\tSalary: "); printf("%f.\n", p[i].Salary);
sum += p[i].Salary; }
avg = sum / N; printf("\nSalary Average: %.2f.\n", avg) ; }
|
|
|
Καταγράφηκε
|
Efstathios Chatzikyriakidis (efxa) Informatics & Communications Engineer (BSc)
WEB: efxa.org - EMAIL: contact@efxa.org
|
|
|
|