Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

data structures and algorithms

Status
Not open for further replies.

nyadimo

Junior Member level 2
Junior Member level 2
Joined
Mar 1, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,427
hi
consider the problem of calculating the average score received on a test by students in each of the four classes: freshman(1st years), sophomore(2nd years), juniors(3rd years), and senoirs(4th years).

1. for each student a score is to be read together with a code 1,2,3 or 4 representing the class to which the student belongs.

2. for each class we must calculate the average score received by the student in the class. to do this, we must find the number of students in each class and sum all the scores for all the students in that class.

now, i am expected to write a program in either C, C++ or JAVA to solve the above problem. i am using an array as my data structure. Please assist me on this question, i am a student learning programming and intrested to be a good programmer in future. thank you.
 

Hi,

there are many ways to solve this problem..
ok, let me explain one way for this..

1. Take an array of structures, lets say of size for example 10...
structure should consists of two fields a. "year" to which the student belongs to
b. "marks" he got..

2. Now print a message,
saying "do you want to add one more record -- y/n".
keep in a while loop, such that, every time the loop goes, you give 'y' or 'n'
if the read character is "y", then continue the loop, else come out of the loop.
each time the loop goes, maintain a loop varible that increments one by one.

each time after you read the option, if the option is "y",
print " enter the year to which student belongs to "
scan it in a variable year,
and assign array_of_structs[loop_variable].year=year;

immediately print "the marks he scored"
read and assign it to array_of_structs[loop_variable].marks=marks.


3. you will come out of this loop once you give option "n".

4. now, go though the whole array -- for each array variable,
check the conditions...

for(i=0; i<loop_var; i++)
{
if(array_of_structs==1)
{
no_of_students_1++;
marks_year_1=marks_year_1+array_of_structs.marks.
}


like the same for four years............
}

5. make sure that, the varibles no_of_students_1,2,3,4 are set to zero before enter the for loop and the same for marks_year_1,2,3,4 also set to zero..

6. after coming out of the loop,
no_of_students_1 gives the no of students in the first year.
marks_year_1 gives the total marks of first year.
like for 2,3,4, also...

7. do division -- avg_year_1 = marks_year_1 / no_of_students_1..
same for 2,3,4, also...

8. print the averages..

9. for better utilization of the array, go for dynamic allocation using malloc() if you know it, by which you can allocate the array size dynamically..

10. i think, it is useful to u, if not reply me, i will write the c code, and will send it to you...

bye
 

    nyadimo

    Points: 2
    Helpful Answer Positive Rating
hi
please write me a c code to that problem so that i can follow the codes carefully. altenatively, you can also give me the c++ code and java code so that i can be conversant with them. thank you.
 

hi, i am attaching the .c file for the above program, try this program, if it solves your problem, its ok, other wise let me know, i will change this again.....

#include<stdio.h>
int main ()
{
typedef struct {int marks; int year;}Struct;
Struct Array[10];
int option=1,total_no_students=0,no_studetns[4]={0}, avg_marks[4]={0};
while(option==1)
{
printf("\nEnter the year to which the student belongs to -- ");
scanf("%d",&Array[total_no_students].year);
printf("Enter the marks for the student -- ");
scanf("%d",&Array[total_no_students].marks);
printf("Do you want to enter one more record -- 1/0 ");
scanf("%d",&option);
total_no_students++;
}

printf("\n\nYou have entered %d number of students record",total_no_students);
total_no_students--;

while(total_no_students>=0)
{
switch(Array[total_no_students].year)
{
case 1:
no_studetns[0]++;
avg_marks[0]+=Array[total_no_students].marks;
break;

case 2:
no_studetns[1]++;
avg_marks[1]+=Array[total_no_students].marks;
break;

case 3:
no_studetns[2]++;
avg_marks[2]+=Array[total_no_students].marks;
break;

case 4:
no_studetns[3]++;
avg_marks[3]+=Array[total_no_students].marks;
break;

}
total_no_students--;
}

printf("\nAnd here is the statistics ");
for(option=0; option<4; option++)
{
printf("\n\nyear -- %d \n",option+1);
printf("No of studetns -- %d\n", no_studetns[option]);
if(no_studetns[option]!=0)
printf("Avg Marks -- %0d\n",avg_marks[option]/ no_studetns[option]);
else
printf("Avg Marks -- NA\n");
}
}
 

hi,
thanks alot for the c code, the program ran without errors, i have studied the program and i am now happy that i can write a program of that kind on my own. thank you.
 

can any one tell me where can i find da solution manual fro data structures n algorithms n C by mark allen weiss 2nd edition
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top