473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

bubbleSort

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <ctype.h>
  5. #define MAX 30
  6.  
  7.  
  8. void getData (FILE*,int[],char[][20],int[],int[],float[],float[],int[],int*);
  9. void computeTotal(int[],int[],float[],float[],int [],int,float[],float[]);
  10. int maxScore(float[],int);
  11. int minScore(float[],int);
  12. float meanScore(float[],int);
  13. void showResult(FILE *,int [],char[][20],int[],int[],float[],int[],int,int,float,int,float[]);
  14. void printLine(FILE *);
  15. void menu();
  16. int seqSearch (int [],int,int);
  17. void bubbleSort(int[],char[][20],float[],int);
  18. void showSortedlist (FILE *,int[],char[][20],float[],int);
  19.  
  20. void main ()
  21. {
  22.    int cnt,id[MAX],mid[MAX],final[MAX],lab[MAX],imax,imin,a,pos,key;
  23.    char name[MAX][20];
  24.    float test1[MAX],test2[MAX],test_avg[MAX],total[MAX],avg;
  25.    FILE *inf,*outf,*outf2;
  26.    inf = fopen ("SCORE.TXT","r");/*open  file*/
  27.    outf = fopen("REPORT.out","w");
  28.    outf2 = fopen("OUTPUT.OUT","w");
  29.    clrscr();
  30.    if (inf == NULL)
  31.    {
  32.       printf("File not found");/*check file for exit*/
  33.       exit(1);
  34.    }
  35.    do{
  36.    menu();
  37.    printf(" Enter No. Menu.........::");
  38.    scanf("%d",&a);
  39.       if (a == 1)
  40.       {
  41.       getData(inf,id,name,mid,final,test1,test2,lab,&cnt);
  42.       }
  43.       else if (a == 2)
  44.       {
  45.       computeTotal(mid,final,test1,test2,lab,cnt,total,test_avg);
  46.       avg = meanScore(total,cnt);
  47.       }
  48.       else if (a == 3)
  49.       {
  50.       imax = maxScore(total,cnt);
  51.       imin = minScore(total,cnt);
  52.       }
  53.       else if (a == 4)
  54.       {
  55.       showResult(outf,id,name,mid,final,test_avg,lab,imax,imin,avg,cnt,total);
  56.       }
  57.       else if (a == 5)
  58.       {
  59.       printf("\n   Searching Student Data\n");
  60.       printf("-----------------------------\n");
  61.       printf("Please Enter Student ID >> ");
  62.       scanf("%d",&key);
  63.       pos = seqSearch (id,key,cnt);
  64.          if (pos ==-1)
  65.             printf(" Not found %d \n",key);
  66.          else 
  67.             printf("%d  &s  &2.f\n",id[pos],name[pos],total[pos]);
  68.       }
  69.       else if (a == 6)
  70.       { 
  71.        bubbleSort(id,name,total,cnt);
  72.        showSortedlist(outf2,id,name,total,cnt);
  73.       }
  74.    } while (a != 0);
  75.  
  76.    fclose(inf);
  77.    fclose(outf);
  78.    getch();
  79. }
  80.  
  81. void getData (FILE*inf,int id[],char name[][20],int mid[],int final[],float test1[],float test2[],int lab[],int*cnt)
  82. {
  83.    *cnt = 0;
  84.    while(!feof(inf))
  85.    {  /* read data from file*/
  86.    fscanf(inf,"%d%s%d%d%f%f%d",&id[*cnt],&name[*cnt],&mid[*cnt],&final[*cnt],&test1[*cnt],&test2[*cnt],&lab[*cnt]);
  87.    (*cnt)++;
  88.    }
  89. }
  90. void computeTotal(int mid[],int final[],float test1[],float test2[],int lab[],int cnt,float total[],float test_avg[])
  91. {
  92.    int i;
  93.    for (i=0;i<cnt;i++)
  94.    {
  95.    test_avg[i] = (test1[i]+test2[i])/2;
  96.    total[i] = mid[i]+final[i]+test_avg[i]+lab[i];
  97.    }
  98. }
  99.  
  100. int maxScore (float total[],int cnt)
  101. {
  102.    int i,max=0,imax=0;
  103.    for(i=0;i<cnt;i++)
  104.    {
  105.       if(total[i]>max)
  106.         {
  107.         max=total[i];
  108.         imax = i;
  109.         }
  110.    }
  111.    return imax;
  112. }
  113.  
  114. int minScore (float total[],int cnt)
  115. {
  116.    int i,min =100,imin=0;
  117.    for(i=0;i<cnt;i++)
  118.    {
  119.       if(total[i]<min)
  120.         {
  121.         min=total[i];
  122.         imin = i;
  123.         }
  124.    }
  125.    return imin;
  126. }
  127.  
  128. float meanScore(float total[],int cnt)
  129. {
  130.    int i;
  131.    float mean,avg;
  132.    for (i=0;i<cnt;i++ )
  133.    {
  134.        mean+= total[i];
  135.    }
  136.     avg = mean/cnt;
  137.     return avg;
  138. }
  139.  
  140. void showResult(FILE *outf,int id[],char name[][20],int mid[],int final[],float test_avg[],int lab[],int imax,int imin,float avg,int cnt,float total[])
  141. {
  142.    int i;
  143.    fprintf(outf,"\t\tIT200   Score  Report\n");
  144.    printLine(outf);
  145.    for(i=0;i<cnt;i++)
  146.    fprintf(outf,"%d   %s \t\t%d  \t%d  \t%.2f  \t%d   \t%.2f\n",id[i],name[i],mid[i],final[i],test_avg[i],lab[i],total[i]);
  147.    printLine(outf);
  148.    fprintf(outf,"Average Score = %.2f\n",avg);
  149.    fprintf(outf,"Max Score ::  %d  %s   \tScore = %.2f\n" ,id[imax],name[imax],total[imax]);
  150.    fprintf(outf,"Min Score ::  %d  %s   \tScore = %.2f\n",id[imin],name[imin],total[imin]);
  151.    printLine(outf);
  152. }
  153.  
  154. void printLine(FILE *outf)
  155.    {
  156.    fprintf(outf,"---------------------------------------------------------------\n");
  157.    }
  158. void menu ()
  159. {
  160.    printf("\n       Grading program\n");
  161.    printf("1> Get Data \n");
  162.    printf("2> Calculate Total Score");
  163.    printf("3> Calculate Max Min Score");
  164.    printf("4> Display Score Repot");
  165.    printf("5> Search Student Data");
  166.    printf("6> Display Student Data Sort by Total Score");
  167.    printf("0> Exit");
  168. }
  169. int seqSearch (int id[],int key,int cnt)
  170. {
  171.     int i;
  172.     printf("...........Searching..............\n");
  173.     for (i=0;i<cnt;i++)
  174.     {
  175.         if (id[i] == key)
  176.         return i;
  177.     }
  178.     return -1;
  179.  
  180. void bubbleSort(int id[],char name [][20],float total [],int cnt)/* Error  Declaration is not allowed here*/
  181. {/*Declaration syntax erroe*/
  182.    int i,j,tempid;
  183.    char tempname[MAX];
  184.    float temptotal;
  185.    for (i=0;i<cnt;i++)
  186.    {
  187.        for (j=cnt-1;j>=i j--)
  188.        {
  189.        if (total[j] < total[j-1])
  190.        {
  191.            temptotal  = total[j-1];
  192.            total[j-1] = total[j];
  193.            total[j]   = temptotal;
  194.  
  195.            strcpy(tempname,name[j-1]);
  196.            strcpy(name[j-1],name[j]);
  197.            strcpy(name[j],tempname);
  198.  
  199.            tempid  = id[j-1];
  200.            id[j-1] = id[j];
  201.            id[j]   = tempid;
  202.        }
  203.     }
  204.   }              
  205. }
  206. void showSortedlist (FILE *outf2,int id[],char name[][20],float total[],int cnt)
  207. {
  208.    int i;
  209.    fprintf(outf2,"     IT200 Score Peport Sort\n");
  210.    printLine(outf2);
  211.    fprintf(outf2,"      Student ID      Name     Total\n");
  212.    for (i=0;i<cnt;i++)
  213.     {
  214.       fprintf(outf2,"       %d           &s         %.2f\n",id[i],name[i],total[i]);
  215.     }
  216.    printLine(outf2);
  217. } /*Errer    Declaration missing ; , Compound statement missing } */
I do not understand why this error bubblesort match with end match pro mix.
Thank you for answers.
Attached Files
File Type: txt SCORE.txt (271 Bytes, 426 views)
Sep 16 '09 #1
3 3616
Banfa
9,065 Expert Mod 8TB
You have a brace missing after line 178
Sep 16 '09 #2
Solving, as was told. But why the results have changed avg bubbleSort results are not sorted, respectively.
Sep 17 '09 #3
Little help to see why the avg bubbleSort first miscalculation was not up to the people but to the last total is 0.

Was also attached with the program running to see with.

Thank you for answers.
Attached Files
File Type: zip program.zip (2.8 KB, 108 views)
Sep 17 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: kathy | last post by:
I have posted my problem before and still not feel confused. Anyone know what is the problem? In my Win32 dll, I defined: #ifdef DllExport #define UNMANAGED_API __declspec(dllexport) #else...
0
by: magic9baller | last post by:
I have to use bubblesort to sort a singly linked list. (Yes I am a n00b.) The tricky part is that I have to move pointers rather than moving the contents of one node to the other. I think my problem...
20
JavaStudent07
by: JavaStudent07 | last post by:
If you know anything about "Bubble Sorts" please leave it here, the teacher said, "Use bubble sort to sort numbers into decreasing numerical value." Then she started eating lunch and said we were on...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
4
by: Trent | last post by:
Still have problems with this thing. Seems my results are not matching the "correct" example given. The three sets of numbers below the last 3 columns is suppose to be the number of comparisons...
2
by: AZRebelCowgirl73 | last post by:
Ok I think I have added the bubbleSort methods correctly I am not getting any errors and the program runs as if I had not added them yet! My question is this! how would I now in the main method...
7
beacon
by: beacon | last post by:
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes to um, sort an array. I have run into some...
16
by: NoviceJava | last post by:
hello, I am having trouble with a program. It seems easy, but for some reason I am just not getting it. I need to create an array of random numbers and call a bubblesort method that I create...
8
by: MLH | last post by:
Is there any built-in FN for returning largest value in a group of say 2 - 10 values? Lets say I want a FN something like MaxVal(5, 7, 9, 3, 12) or perhaps MaxVal(79,34). I would wanna see return...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.