How to design bar chart in c - c

I have created the program and generate code below
#include <stdio.h>
int main(){
int a,i;
scanf("%d",&a);
while(a!=-1){
if(a>=0 && a<=80){
for(i=a;i<=a;i++)
printf("|");
printf("%d\n");
scanf("%d",&a);
}
}
with input 1 3 4 4 5 5 -1, it should be display a bar chart like
|
| | |
| | | |
| | | |
| | | | |
| | | | |
but in that codes ,display
|
|
|
|
|
|
can anybody explain why its not working?

for(i=a;i<=a;i++)
^ maybe you want 0 here?
printf("%d\n"); also looks wrong, %d is a formate string uses to print value of int. To print simply newline char it should be just printf("\n");

Related

viewing the variable stored in an C array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
The community is reviewing whether to reopen this question as of 5 months ago.
Improve this question
I am making a code that reads the data every time a new frequency is applied.
(I have a list of frequencies in an array)
I would like to display at the start of the set of readings a text saying current frequency (Hz): and then the value defined in the following array int freq[] = {1000 , 10000 , 30000 } . I am indexing the different frequencies in a for loop with the following code
for (byte count = 0; count < 6; count = count + 1) {
Serial.print("current Frequency(hz): ");
Serial.println( freq[count], 4);
However when I open the serial monitor I get numbers that I have not defined as a in the freq array. What am I doing wrong.
It takes the 4 least significant bits of cmd and the 10 least significant bits of data and combines them as follows:
cmd data
+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+---+
... | | | | | ... | | | | | | | | | | |
+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | | | | |
| | | +---------+ | | | | | | | | | |
| | +---------+ | | | | | | | | | | |
| +---------+ | | | | | | | | | | | |
+---------+ | | | | | | | | | | | | |
| | | | | | | | | | | | | |
v v v v v v v v v v v v v v
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 0 | 0 | | | | | | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
data_word

Drawing a bar graph in C

The user should input the elements of an array(maximum 20 of them). The task is to draw a bar graph using |, *, and - by using these elements. A line of minuses should be drawn instead of zero, and * instead of last member of an array. If the number is 3 for example, two | and one * above them should be drawn. The problem that I have with my code is that it sometimes takes too long to execute, draws one instead of two minuses between the array elements, and sometimes doesn't draw the line with minuses.
#include <stdio.h>
int main() {
int n[20];
int i,j,counter=0,input=0,min,max;
printf("Elements of the array: \n");
while(input!=1000){
scanf("%d", &input);
n[counter]=input;
counter++;
}
counter--;
min=n[0];
max=n[0];
for(i=1;i<counter;i++){
if(n[i]>max) max=n[i];
if(n[i]<min) min=n[i];
}
for(i=0;i<max-min+1;i++){
for(j=0;j<2*counter;j++){
if(j%2==0 && n[j/2]==max-i) printf("*");
else if(i==max) printf("-");
else if(j%2==0 && n[j/2]>0 && i<max && n[j/2]>max-i) printf("|");
else if(j%2==0 && n[j/2]<0 && i>max && n[j/2]<max-i) printf("|");
else printf(" ");
}
printf("\n");
}
}// Input:4 -3 7 0 -1 1000 Expected output:
*
|
|
* |
| |
| |
| |
---------*-----
| *
|
*
My output:
*
|
|
* |
| |
| |
| |
------*---
| *
|
*
Input: 5 4 3 2 1 2 3 4 5 Expected output:
* *
| * * |
| | * * | |
| | | * * | | |
| | | | * | | | |
---------------------------
My output:
* *
| * * |
| | * * | |
| | | * * | | |
| | | | * | | | | //

What happens when I add character array with unsigned long in c?

#include <stdio.h>
#include<string.h>
void double_string(char ary[])
{
char *start = ary;
// dont exactly know what is happening here nothing is getting printed when consoled
char *end = ary + strlen(ary);
char *org = end;
while(start<org)
{
*end = *start;
start++;
end++;
}
*end = '\0';
}
int main(void) {
char word[255] = {"TACsasasa"};
double_string(word);
printf("%s",word);
return 0;
}
I am unable to understand what is getting stored in character array "*end", I tried printing it but I am not getting any output printed..
char *end = ary + strlen(ary);
This line of code is taking the starting address of the char array in memory, adding the number of bytes inside the array, returned by strlen(), and essentially moving the pointer to the end. The end of the array is the null terminator. Printing that will show nothing as it takes it as an empty string.
Adding an unsigned int (or even a signed int!) is known as pointer arithmetic. It's legal and quite common in C code. You do have to be very careful about going out of the bounds of your memory buffer, though, or you will experience undefined behavior. This is bad. Fortunately, this code appears to be quite well behaved as long as the original string is less than half the length of its memory buffer.
Allow me to try some ASCII art to see if I can make clear what's going on in the double_string function. It starts with this:
char *start = ary;
char *end = ary + strlen(ary);
char *org = end;
At this point, your pointers look like this:
start end
| |
| org
| |
v V
----------------------------------------------------------------------------------
ary | T | A | C | s | a | s | a | s | a |\0 | | | | | | | | | | | ...
----------------------------------------------------------------------------------
Then we have the loop.
while(start<org)
{
*end = *start;
start++;
end++;
}
After the first loop iteration, it looks like this:
start end
| |
| org |
| | |
v V V
----------------------------------------------------------------------------------
ary | T | A | C | s | a | s | a | s | a | T | | | | | | | | | | | ...
----------------------------------------------------------------------------------
Second iteration:
start end
| |
| org |
| | |
v V V
----------------------------------------------------------------------------------
ary | T | A | C | s | a | s | a | s | a | T | A | | | | | | | | | | ...
----------------------------------------------------------------------------------
And so on. The loop continues as long as start is less than (to the left of, in my illustration) org:
start end
| |
org |
| |
V V
----------------------------------------------------------------------------------
ary | T | A | C | s | a | s | a | s | a | T | A | C | s | a | s | a | s | a | | | ...
----------------------------------------------------------------------------------
Now start<org is no longer true, because they're equal. They point to the same location. The loop terminates. All that's left to do is terminate the string with *end = '\0';:
start end
| |
org |
| |
V V
----------------------------------------------------------------------------------
ary | T | A | C | s | a | s | a | s | a | T | A | C | s | a | s | a | s | a |\0 | | ...
----------------------------------------------------------------------------------

Segmentation Core dumped

| |0|1|2|3|4|5|6|7|8|9|
|0| | | | | | | | | | |
|1| | | | | | | | | | |
|2| | | | | | | | | | |
|3| | | | | | | | | | |
|4| | | | | | | | | | |
|5| | | | | | | | | | |
|6| | | | | | | | | | |
|7| | | | | | | | | | |
|8| | | | | | | | | | |
|9| | | | | | | | | | |
I am trying to make a grid that looks like this. I currently keep getting Segmentation Fault Core dumped whenever I try to run the executable. The compiler is also not showing any errors. I am not sure how to print the numbers inside the grid either. Below is the code I currently have (just a part of the whole assignment). Any help is greatly appreciated.
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
/* TODO */
int i,j;
char grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2];
for(i = 0; i < BOARD_HEIGHT; i++)
{
for(j = 0; j < BOARD_WIDTH; j++)
{
grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2] = '|';
printf("%c", grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2]);
printf("%s", EMPTY_OUTPUT);
}
printf("\n");
}
}
The BOARD_HEIGHT and BOARD_WIDTH are variables defined in a header file.
#ifndef BOARD_H
#define BOARD_H
#include "helpers.h"
#include "player.h"
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 10
typedef enum cell
{
EMPTY,
BLOCKED,
PLAYER
} Cell;
#define EMPTY_OUTPUT " "
#define BLOCKED_OUTPUT "*"
Cell BOARD_1[BOARD_HEIGHT][BOARD_WIDTH];
Cell BOARD_2[BOARD_HEIGHT][BOARD_WIDTH];
typedef enum playerMove
{
PLAYER_MOVED,
CELL_BLOCKED,
OUTSIDE_BOUNDS
} PlayerMove;
void initialiseBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH]);
void loadBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Cell boardToLoad[BOARD_HEIGHT][BOARD_WIDTH]);
Boolean placePlayer(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Position position);
PlayerMove movePlayerForward(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Player * player);
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player);
#endif
this second part is in the header file.
The full code can be found here a1
Your bug is on the line where you index grid beyond the end of the array:
grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2] = '|'; // Bug
This line writes beyond the end of the grid array defined locally as:
char grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2];
Note that the valid indexes into grid[][] go from 0 to BOARD_HEIGHT+1, and 0 to BOARD_WIDTH+1. Your code is accessible one past this in both dimensions.
This results in corrupting the stack, so when the displayBoard function returns, it has corrupted the stack and you get a segmentation fault. Removing this line eliminates the seg fault, but the code still needs work for function correctly.
You should reference the board array, and use indexes i and j, not hardcoded constants.
Here is a working version where I index into the board array, store the current cell value in currentCell, then display it based on a switch statement. This produces the following output given an empty board filled with zeroes:
scott> gcc -g -ogeraldTest -O0 board.c
scott> geraldTest
| |0|1|2|3|4|5|6|7|8|9|
|0| | | | | | | | | | |
|1| | | | | | | | | | |
|2| | | | | | | | | | |
|3| | | | | | | | | | |
|4| | | | | | | | | | |
|5| | | | | | | | | | |
|6| | | | | | | | | | |
|7| | | | | | | | | | |
|8| | | | | | | | | | |
|9| | | | | | | | | | |
Here is the code for main() and printBoard, both in board.c. Note that printBoard is passed the board by value, but it would be better to pass it by reference.
#include <stdio.h>
#include "board.h"
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
int i,j;
// First display the header at the top.
printf("| |");
for(j=0; j<BOARD_WIDTH;j++)
printf("%d|",j);
printf("\n");
for(i = 0; i < BOARD_HEIGHT; i++)
{
// Display each row number
printf("|%d|",i);
for(j = 0; j < BOARD_WIDTH; j++)
{
// Bug: grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2] = '|';
Cell currentCell = board[i][j];
switch(currentCell) {
case BLOCKED:
printf("%s",BLOCKED_OUTPUT);
break;
case PLAYER:
printf("P");
break;
default:
printf("%s",EMPTY_OUTPUT);
break;
}
printf("|");
// This code is wrong
// printf("%c|", grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2]);
// printf("%s", EMPTY_OUTPUT);
}
printf("\n");
}
}
int main(int argc, char ** argv)
{
Cell myBoard[BOARD_HEIGHT][BOARD_WIDTH] = {0};
Player bob = 0;
displayBoard(myBoard, &bob);
}
I took your board.h and defined the missing types in order to get it to compile. You can likely just use your own headers as is.
#ifndef BOARD_H
#define BOARD_H
// #include "helpers.h"
// #include "player.h"
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 10
typedef int Boolean; // Added
typedef int Player; // Added
typedef struct { // Added
int row;
int col;
} Position;
typedef enum cell
{
EMPTY,
BLOCKED,
PLAYER
} Cell;
#define EMPTY_OUTPUT " "
#define BLOCKED_OUTPUT "*"
Cell BOARD_1[BOARD_HEIGHT][BOARD_WIDTH];
Cell BOARD_2[BOARD_HEIGHT][BOARD_WIDTH];
typedef enum playerMove
{
PLAYER_MOVED,
CELL_BLOCKED,
OUTSIDE_BOUNDS
} PlayerMove;
void initialiseBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH]);
void loadBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Cell boardToLoad[BOARD_HEIGHT][BOARD_WIDTH]);
Boolean placePlayer(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Position position);
PlayerMove movePlayerForward(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Player * player);
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player);
#endif

Printing two 2D arrays side by side

I've just started doing a unit of programming in C and my first assignment is to make a simple console version of the classic Battleship game.
A part of this - and the part I'm having trouble with at the moment - is displaying information to the player at each turn.
This is what we've been asked to have our display look like:
And, with a minimum of fuss, I've got mine looking like this:
(My Player grid is empty at the moment because its 2D array is empty, but if I use an array with something in it, it's fine.)
My header file:
/* Header files. */
#include <stdio.h>
#include <stdlib.h>
#define UNKNOWN ' '
#define SIZE 10
/* Function prototypes. */
void init(char playerHidden[SIZE][SIZE], char playerReveal[SIZE][SIZE],
char computHidden[SIZE][SIZE], char computReveal[SIZE][SIZE]);
void displayKnownInfo(char playerReveal[SIZE][SIZE],
char playerHidden[SIZE][SIZE],
char computReveal[SIZE][SIZE]);
My C source file:
#include "bship.h"
int main(void)
{
/* Stores player ship position information secret to opponent. */
char playerHidden[SIZE][SIZE];
/* Stores player ship position information known to opponent. */
char playerReveal[SIZE][SIZE];
/* Stores computer ship position information secret to opponent. */
char computHidden[SIZE][SIZE];
/* Stores computer ship position information known to opponent. */
char computReveal[SIZE][SIZE];
init(playerHidden, playerReveal,
computHidden, computReveal);
displayKnownInfo(playerReveal, playerHidden, computReveal);
return EXIT_SUCCESS;
}
/****************************************************************************
* Function init() initialises every cell in the four grids to a safe default
* value. The UNKNOWN constant is used for the initialisation value.
****************************************************************************/
void init(char playerHidden[SIZE][SIZE], char playerReveal[SIZE][SIZE],
char computHidden[SIZE][SIZE], char computReveal[SIZE][SIZE])
{
/*Variables i and j for each dimension of the Arrays*/
int x,y;
/*For each increment BETWEEN 0 and 'SIZE', firstly for i;*/
for(y=0; y<SIZE; y++)
{
/*And then for j;*/
for(x=0; x<SIZE; x++)
{
/*Populate that cell with the constant UNKNOWN*/
playerHidden[x][y]=UNKNOWN;
playerReveal[x][y]=UNKNOWN;
computHidden[x][y]=UNKNOWN;
computReveal[x][y]=UNKNOWN;
}
}
}
/****************************************************************************
* Function displayKnownInfo() presents revealed information about the game in
* the format below. In this example, both contestants have made five
* guesses.
* As you can see, the computer player got lucky with all five guesses and has
* sunk the human players' aircraft carrier. The identity of the ship was
* revealed when the aircraft carrier was HIT the fifth time.
* The human player has been less lucky. The first four guesses were a MISS.
* However, the fifth guess was a HIT on the computer players' submarine. The
* human player does not yet know the identity of this ship yet as it is still
* afloat.
* All other squares are still UNKNOWN.
*
* Player | Computer
* 1 2 3 4 5 6 7 8 9 0 | 1 2 3 4 5 6 7 8 9 0
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* a |A| | | | | | | | | | | a | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* b |A| | | | | | | | | | | b | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* c |A| | | | | | | | | | | c | | | | | | |=| | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* d |A| | | | | | | | | | | d | | |x| | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* e |A| | | | | | | | | | | e | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* f | | | | | | | | | | | | f | | | | |=| | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* g | | | | | | | | | | | | g | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* h | | | | | | | | | | | | h | |=| | | | |=| | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* i | | | | | | | | | | | | i | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* j | | | | | | | | | | | | j | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+
* Aircraft Carrier (5/5) | 0/5 ships sunk.
* Battleship (0/4) | 1 hits.
* Destroyer (0/3) | 4 misses.
* Frigate (0/3) |
* Submarine (0/2) |
****************************************************************************/
void displayKnownInfo(char playerReveal[SIZE][SIZE],
char playerHidden[SIZE][SIZE],
char computReveal[SIZE][SIZE])
{
/*Ints for stepping through the arrays*/
int i,j;
/*First row identifier*/
char row='a';
/*Printing first few lines.*/
printf(" Player | Computer\n");
printf(" 1 2 3 4 5 6 7 8 9 0 | 1 2 3 4 5 6 7 8 9 0\n");
printf(" +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+\n");
printf(" %c |", row);
/*Loop through the arrays*/
for(i=0; i<SIZE; i++)
{
for(j=0; j<SIZE; j++)
{
/*Print the char at [i][j] with a '|' afterwards.*/
printf("%c|",playerReveal[i][j]);
}
/*After reaching column '0' on the display, increment the row identifier*/
row++;
/*And print the 'spacer' row.*/
printf("\n +-+-+-+-+-+-+-+-+-+-+ | +-+-+-+-+-+-+-+-+-+-+");
/*If the current row identifier is less than 'k', we want to print it*/
if(row<'k')
{
printf("\n %c |",row);
}
else
{
printf("\n");
}
/*And continue until the array has been printed in full.*/
}
}
}
Eventually, the PlayerHidden array will be overlaid on the PlayerReveal array when a player's ship has been sunk and its identity 'revealed', but for now I'm just wanting to get the computer half of the display working.
Just add this to your code after the second loop where you prints stuff:
printf(" %c |", row);
for(j=0; j<SIZE; j++)
{
/*Print the char at [i][j] with a '|' afterwards.*/
printf("%c|",computReveal[i][j]);
}
So your code looks like:
//...
/*Loop through the arrays*/
for(i=0; i<SIZE; i++)
{
for(j=0; j<SIZE; j++)
{
/*Print the char at [i][j] with a '|' afterwards.*/
printf("%c|",playerReveal[i][j]);
}
printf(" %c |", row);
for(j=0; j<SIZE; j++)
{
/*Print the char at [i][j] with a '|' afterwards.*/
printf("%c|",computReveal[i][j]);
}
//...
Your problem is that you're only printing out the player portion. Take a look at the code:
for(i=0; i<SIZE; i++)
{
for(j=0; j<SIZE; j++)
{
/*Print the char at [i][j] with a '|' afterwards.*/
printf("%c|",playerReveal[i][j]);
}
// ...
}
You need to have another loop where you print out the computerReveal array
for(i=0; i<SIZE; i++)
{
for(j=0; j<SIZE; j++)
{
/*Print the char at [i][j] with a '|' afterwards.*/
printf("%c|",playerReveal[i][j]);
}
for(j=0; j<SIZE; j++)
{
/*Print the char at [i][j] with a '|' afterwards.*/
printf("%c|",computReveal[i][j]);
}
// ...
}
That should do it. You're printing out the full width row separators, but not the portion of the array that creates the cell separators

Resources