Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a C program that does the following
- Generates 10 random quiz scores, stores them in the one-dimensional array quizscore, and displays the scores. All scores must be whole numbers and that the highest and lowest possible scores are 30 (perfect score) and 15, respectively.
- Sorts the array quizscore and displays the sorted scores in decreasing order.
- Computes the percentages of the scores and stores in the one-dimensional array grades to their equivalent grades based on the following:
-  90 to 100 = 1.0
- 80 to below 90 = 1.5
- 70 to below 80 = 2.0
- 60 to below 70 = 2.5
- Below 60 = 3.0
- Displays a histogram (graph showing frequency) of the grades.

Functions that must be used in the program:
- void printArray(int a[], int s) that displays the elements of an integer array a of size s in columns of 10
- void sortArray(int a[], int s) that sorts an integer array a of size s in decreasing order
- srand(time(NULL)) to randomize the pseudorandom numbers of rand()

HISTOGRAM FORMAT
1.0: xxxxxxxxxxxxxx
1.5: xxxxxxxxxxxxxxxxxx
2.0: xxxxxxx
2.5: xxxxxx
3.0: xxxx


What I have tried:

Please don't judge me, I am new to this

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10

int main()
{
    int bubble[SIZE];
    int inner,outer,temp,x;

    srand(time(NULL));

    puts("Original Array:");
    for(x=0;x<SIZE;x++)
    {
        bubble[x] = rand();
        bubble[x] = (bubble[x] % 30 - 15 + 1)+ 15;
        printf("%d\t",bubble[x]);
    }
    putchar('\n');

/* Bubble sort */
    for(outer=0;outer<SIZE-1;outer++)
    {
        for(inner=outer+1;inner<SIZE;inner++)
        {
            if(bubble[outer] > bubble[inner])
            {
                temp=bubble[outer];
                bubble[outer] = bubble[inner];
                bubble[inner] = temp;
            }
        }
    }


    puts("Sorted Array:");
    for(x=0;x<SIZE;x++)
        printf("%d\t",bubble[x]);
    putchar('\n');

    return(0);
}
Posted
Updated 23-Nov-22 4:22am
v3
Comments
jeron1 22-Nov-22 12:35pm    
What is your question?
jeron1 22-Nov-22 13:02pm    
You'll need variables to hold the counts (5 of them). So you iterate through the array, calculate the percentage for a given grade, then increment the appropriate count variable based on that percentage, perhaps using if, elseif, else. When complete, print the appropriate label for example "1.0" or "1.5", then create a loop (a for loop maybe) based on the associated count variable to print the x's.

The next step is to calculate the size of the bins for the histogram. This is the part where the scores get values that range from 1.0 to 3.0 in five bins or slots in an array of integers. The counts in each bin determine how many X's are displayed and that is the next step : displaying the bins. For each test in a bin you display one X so you have to make a loop to draw the Xs according to the count in that bin. It's not going to look quite like the sample because you have only ten tests.

Here's a piece of code that determines what slot a test goes into :
C
int testPercent = 100 * testScore / 30;
if( testPercent >= 90 )
    ++bins[ 0 ];
else if( testPercent >= 80 )
    ++bins[ 1 ];
// ... and so on for each bin
I hope you can see the pattern that the bins stored are in the same order they are displayed. That means bin 0 is the first row, bin 1 is second, and so on.
 
Share this answer
 
In the original post, the range was not respected, but that seems to have been noticed already.

To generate random numbers in a range you could write a function random(min, max).
It would be best to write functions for all single tasks.
The main program would then be much easier to read.

C
int bubble[SIZE];

srand((unsigned)time(NULL));
arrgen(bubble, SIZE);

outputarr(bubble, SIZE);
bubblesort(bubble, SIZE);
outputarr(bubble, SIZE);
outputhist(bubble, SIZE);
 
Share this answer
 
Here's what I have tried again,

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int lower = 20, upper = 40, count = 50, examrawscore[50], temp, i, j;
    
    srand(time(0));

    printf("Exam scores: \n");
    for (i = 0; i < count; i++) 
    {
         int num = (rand() % (upper - lower + 1)) + lower;
         //storing yung value ng num sa array
		examrawscore[i] = num;
        
    }
    
    //sorting array into descending order
    
    for (i = 0; i < count; i++)
    {
    	for (j = i + 1; j < count; j++)
    	{
    		if(examrawscore[i] < examrawscore[j])
    		{
    			temp = examrawscore[i];
    		examrawscore[i] = examrawscore[j];
    			examrawscore[j] = temp;
			}
    	}
	}
	
	for (i = 0; i < count; i++)
	{
		printf("%d ", examrawscore[i]);
	}
	
	int testScore, bins;
	int testPercent = 100 * testScore / 40;
	testScore = examrawscore[i];
	
        if( testPercent >= 90 )
        ++bins[ 0 ];
        else if( testPercent >= 80 )
        ++bins[ 1 ];
        else if( testPercent >= 70 )
        ++bins[ 2 ];
        else if( testPercent >= 60 )
        ++bins[ 3 ];
        else if( testPercent < 60 )
        ++bins[ 4 ];
    
    return 0;
}
 
Share this answer
 
Comments
jeron1 23-Nov-22 10:07am    
First, you should update your original post with this attempt and not create a 'solution'. Second. you define an int variable 'bins', that are are treating like an array of int's, that can't work. If you want an array of integers declare and initialize one;

int bins[5] = { 0, 0, 0, 0, 0 };
merano99 23-Nov-22 10:25am    
Please do not post a solution to your question here yourself. Better would be to add to the question above already found solutions with appropriate reference.
Maybe it would be better to mark the helpful solutions here as a solution and give an appropriate rating. Then ask a follow-up question with new questions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900