Following program was asked by Josh Software during campus recruitment.
Write a program to generate following chart without using any graph library.This is of marks from 1 to 5 obtained by 10 student represented by an array as [2,4,5,1,2,4,1,1,5,2]
Y axis denote marks and X axis denotes student role number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include<stdio.h> //www.GauravVichare.com void main() { int a[]={2,4,5,1,2,4,1,1,5,2}; int i,mx,n=10; mx=5; printf("\n |\n"); while(mx) { printf("{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d|",mx);//Y -axis and Y co-ordinates for(i=0;i<n;i++) { if(a[i]>=mx) { printf(" * "); } else { printf(" "); } } printf("\n"); mx--; } printf("0|---------------------------------------------------\n ");//X -axis for(i=1;i<=n;i++) printf(" {b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d ",i);// X co-ordinates //www.GauravVichare.com } |
Variation of problem:
Write a program to generate graph (Roll number Vs marks obtained) without using any graph library. Accept marks of n student from user, represented by an array . Y axis denote marks and X axis denotes student role number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include<stdio.h> #include<malloc.h> //www.GauravVichare.com int maxim(int [],int ); void print(int [],int ); void main() { int *a,n,i; printf("Enter total number of elements in array:"); scanf("{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",&n); a=(int*)malloc(n*sizeof(int)); printf("\nEnter elements:"); for(i=0;i<n;i++) { scanf("{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",&a[i]); } print_graph(a,n); } void print_graph(int a[],int n) { int i,mx; mx=maxim(a,n); //find maximum from array, i.e. length of Y axis printf("\n |\n"); while(mx) { printf("{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}5d|",mx); for(i=0;i<n;i++)//scan complete array { if(a[i]>=mx) // { printf(" * "); } else { printf(" "); } } printf("\n");//next row mx--; } //print X -axis and co-ordinates printf(" 0|"); for(i=0;i<n;i++) printf("-----"); printf("\n "); for(i=1;i<=n;i++) printf(" {b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d ",i); } int maxim(int a[],int n)//find maximum from array, i.e. length of Y axis { int mx=a[0],i; for(i=1;i<n;i++) if(mx<a[i]) mx=a[i]; return mx; } |
If you have any problem with code,please comment below.
Recent Comments