ProductsLogo
LogoStudy Documents
LogoAI Grader
LogoAI Answer
LogoAI Code Checker
LogoPlagiarism Checker
LogoAI Paraphraser
LogoAI Quiz
LogoAI Detector
PricingBlogAbout Us
logo

C Programming: Stack Implementation and Score Analysis

Verified

Added on  2020/05/16

|11
|1305
|31
AI Summary
This C programming assignment involves two distinct tasks. The first task focuses on implementing a stack data structure using an array. Students are required to define functions for pushing elements onto the stack (Push(X)) and popping elements off the stack (Pop(Y)). The second task deals with score analysis. Given a set of scores, students need to analyze and categorize them into different ranges using histograms. The assignment tests understanding of C programming concepts like arrays, stacks, and data visualization techniques.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Data Structure
Name of the student:
Name of the University:
Author note:

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Table of Contents
Question 1 Scores Program.............................................................................................................3
Source Code.................................................................................................................................3
Output Screenshot........................................................................................................................8
Question 2 Stack Program...............................................................................................................9
Source code..................................................................................................................................9
Output Screenshot......................................................................................................................11
Document Page
Question 1 Scores Program
Problem Statement
Write a complete C program which uses an array application. The program will input
mid-semester test scores which are kept in a Main Scores Array. Valid test scores ranges from 0
to 100. Invalid test scores are below 0 and above 100, which is out of the valid score range. An
error message is displayed when an invalid test score is entered. The valid test scores are kept in
a Valid Scores Array, and the invalid test scores are kept in a Invalid Scores Array.
The program will produce six outputs:
1. Valid scores.
2. Invalid scores.
3. The average, lowest and highest of the valid scores.
4. A histogram, giving for each valid score a bar whose length is proportional to the number
achieving that score.
The total number of invalid scores.
Source Code
#include<stdio.h>
#define size 100 //define array max size
//histogram function creates the histogram by comparing data against the range in arguments
void histogram(int arr[size], int valSize, int lRange, int uRange)
{
printf(" [%d - %d]:\t",lRange,uRange);
Document Page
int i;
for(i=0;i<valSize;i++)
{
if(arr[i]>=lRange && arr[i]<=uRange)
printf("x");
}
printf("\n");
}
//function main
void main()
{
int i,n, invalCount=0, valCount=0, max ,min;
float sum=0.0, avg;
int mainScores[size], validScores[size], invalidScores[size];
while(1)
{
printf("How many marks to enter (from 1 to %d)?? .. ",size);
scanf("%d",&n);
if(n>size || n<1) //check if user enters a valid limit
{
printf("Wrong entry!!\n");
}
else
{
break;
}
}
for(i=0;i<n;i++)
{

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
printf("Enter mark: ");
scanf("%d",&mainScores[i]); //input marks into main array
}
for(i=0;i<n;i++)
{
if(mainScores[i]<0 || mainScores[i]>100) //check for invalidity of scores entered
{
invalidScores[invalCount++]=mainScores[i]; //insert element into invalid
array
}
else //check for validity of scores entered
{
validScores[valCount++]=mainScores[i]; //insert element into valid array
}
}
printf("\n\nThe MID-SEMESTER Test Scores\n");
//print all scores
printf("\nAll records: ");
for(i=0;i<n;i++)
{
printf("%d ",mainScores[i]);
}
//print valid scores only
printf("\n\nValid Scores: ");
for(i=0;i<valCount;i++)
{
printf("%d ",validScores[i]);
Document Page
}
//print invalid scores only
printf("\nInvalid Scores: ");
for(i=0;i<invalCount;i++)
{
printf("%d ",invalidScores[i]);
}
//calcualte sum of all scores in valid array and then finalize average score
for(i=0;i<valCount;i++)
{
sum += validScores[i];
}
avg=sum/(valCount);
printf("\n\nAverage score: %f",avg);
//check for max score
max=validScores[0];
for(i=1;i<valCount;i++)
{
if(validScores[i]>max)
max=validScores[i];
}
printf("\nHighest Score: %d",max); //print max score
//check for min score
min=validScores[0];
for(i=1;i<valCount;i++)
{
if(validScores[i]<min)
Document Page
min=validScores[i];
}
printf("\nLowest Score: %d",min); //print min score
printf("\nHistogram: \n");
histogram(validScores,valCount,80,100);
histogram(validScores,valCount,60,79);
histogram(validScores,valCount,50,59);
histogram(validScores,valCount,30,49);
histogram(validScores,valCount,0,29);
printf("\nTotal number of invalid scores: %d\n",invalCount);
}

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Output Screenshot
Document Page
Question 2 Stack Program
Problem statement
Write a C program that creates a Stack A with the values to be read from the entered text,
then invokes Push(X) and Pop(Y) functions respectively. Push(X) and Pop(Y) of the Stack A
should be performed using the respective pointers or arrays, and displays the results
Source code
#include<stdio.h>
#include<string.h>
#define size 100 //define array max size to constant
//push operation
int push(char c, char a[size], int top)
{
a[top++]=c;
return top;
}
//pop operation
int pop(char a[size],int top)
{
return a[top];
}
//main function
void main()
{
int i,j,top=0,tempTop,x=0;
Document Page
char str[size],A[size],temp[size];
printf("Enter TEXT: ");
gets(str); //take user input of text
printf("\n\n");
printf("Initial stack-A printing: ");
printf("\n\n");
for(i=0;i<strlen(str);i++)
{
top=push(str[i],A,top); //push each character of text into stack A
}
printf("BeginStack ");
//display initial state of stack A
for(i=top-1;i>=0;i--)
{
printf("-> %c ",A[i]);
temp[i]=A[i]; //copy elements of stack A from top to temporary stack-array
temp[]
}
printf("-> EndStack\n");
tempTop=top;
top=0;
while(tempTop>0)
{
top=push(pop(temp,--tempTop),A,top); //pop elements from temporary stack
temp and push into main stack A
}
printf("\nFinal stack-A printing: ");
printf("\n\n");
printf("BeginStack ");

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
//display final stack A
for(i=top-1;i>=0;i--)
{
printf("-> %c ",A[i]);
}
printf("-> EndStack\n");
}
//end of main functions
Output Screenshot
1 out of 11
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]