Hello everyone and thanks for taking your time to read this. I am doing an exercise where I'm given an array of integers and each integer represents the "height" of one bar in the chart. Negative values mean that the bar extends below the horizontal axis. So, if I am given an array of 4 integers, there would exactly be 4 bars in the chart.
I used a 2D array of characters to try to complete this exercise.
note: the text of the exercise says that there should be two spaces between each bar in the graph, so that's why I increment the j control variable by three in each pass.
I am having problems with the negative values. No matter what I do, the negative values won't print out on the screen.
What I have tried is the following: I determined the width of the graph by multiplying the array size by three. I then set all fields of the 2D array to spaces except for the horizontal line in the middle.
The outer, j loop iterates through the columns of the matrix. I have only one loop for this because, in every pass through the j loop we start in the 10th row.
the k loop actually does the plotting. The loop goes from zero to number-1, leaving a space for the asterisk at the top (or bottom).
I have two if statements checking if the number is <0 or >0, because in those two instances the bar is supposed to show up differently.
Here is my code:
#include <stdio.h>
int main()
{
char mat[30][40];
int i,j, size=0, m=20, n=40;
int arr[7] = {1,2,-3,4,-5,6,-7};
size=7;
n=size*3; // n is the width of the graph
for(i=0; i<m; i++) { // I set all fields of the 2D array to spaces
for(j=0; j<n; j++) { // except for the horizontal line
if(i==10) mat[i][j]='-';
else mat[i][j]=' ';
}
}
int k;
int position=0; // position is the variable I use to iterate through the array
int l;
for(j=0; j<n; j+=3) {
l=10;
for( k = 0; k<arr[position]-1; k++) {
if(arr[position]==0) {
mat[10][j] = '*'; // if the height is zero, put an asterisk
}
else if((arr[position])<0) {
l++; // if the number is negative,
mat[l][j]='|'; // the bar extends below the horizontal line
}
else if(arr[position]>0) {
l--; // if the number is positive, the bar extends above the
mat[l][j] = '|'; // horizontal line
}
}
if(arr[position]>0) mat[l-1][j]='*'; //every bar ends with an asterisk
else if(arr[position]<0) {
mat[l+1][j]='*';
}
position++; // jump to the next element in array
}
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
printf("%c",mat[i][j]);
}
printf("\n");
}
return 0;
}
For negative numbers it just does not work. Furthermore I have made some tests and the condition else if((arr[position])<0)
is never fulfilled and I don't know why. What it does is it puts an asterisk below the horizontal line but that's about it.
Thank you for reading!
The problem is this:
for( k = 0; k<arr[position]-1; k++)
If the arr[position] is less than 2 than nothing will happen. Try this
.
.
.
l=10;
int end = arr[position] < 0 ? -arr[position] : arr[position]; // added code
for( k = 0; k < end - 1; k++) { // changed code
if(arr[position]==0) {
mat[10][j] = '*'; // if the height is zero, put an asterisk
}
.
.
.
Output
*
|
* |
| |
* | |
* | | |
---------------------
| | |
| | |
* | |
| |
* |
|
*
Basically taking the absolute value of arr[position] and using that as an end condition.
EDIT:
To make astrix appear when the arr[position] is 0. Do the following:
.
.
.
int end = arr[position] < 0 ? -arr[position] : arr[position];
if(arr[position]==0) {
mat[10][j] = '*'; // if the height is zero, put an asterisk
}
for( k = 0; k < end - 1; k++) {
.
.
.
So moving out the if from the for loop since if arr[position] is 0 than the loop does not execute.
You can use for( k = 0; k < abs(arr[position]) - 1; k++) otherwise if arr[position] is a negative number or 0 the loop won't execute when there are negative numbers int the arr[position].
#include <stdlib.h> is needed.
You also need to move clause for the 0 values to the outer for loop for them to be printed on the graph.
Working sample
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With