#include <stdio.h>
#include <stdlib.h>
int main()
{
char mat[30][40];
int i,j, size=0, m=20, n=40;
int arr[7] = {1,0,-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;
if(arr[position] == 0) {
mat[10][j] = '*'; // if the height is zero, put an asterisk
}
for( k = 0; k < abs(arr[position]) - 1; k++) { // changed code
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;
}