print equally spaced elements of a 2D array with printf - c

I am trying to print a 2d array that has a maximum of 3 digit numbers that are aligned when printed. For example, with a simple printf, it looks like this:
[0, 232, 20, 96, 176, 0, 0]
[0, 0, 24, 0, 0, 176, 0]
[0, 0, 0, 0, 0, 0, 0]
I would like it to be printed with all the commas aligned along the columns with additional whitespace, like this:
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]
How can I do this with printf?

You can use the width prefix to specify the minimum width for the printf conversions: printf("%4d", x); will print int variable x padded to the left with enough spaces to produce at least 4 characters.
If you know the maximum width of any number in the array, you can hardcode this number in the format string. Otherwise you can compute the required width and use %*d and pass an extra argument to specifying the computed width.
Here is a modified version:
#include <stdio.h>
int main(void) {
#define M 3
#define N 7
int a[M][N] = {
{ 0, 232, 20, 96, 176, 0, 0 },
{ 0, 0, 24, 0, 0, 176, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
};
int width = 0;
/* compute the required width */
for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) {
int w = snprintf(NULL, 0, "%d", a[i][j]);
if (width < w) {
width = w;
}
}
}
/* print the arrays */
for (size_t i = 0; i < M; i++) {
printf("[");
for (size_t j = 0; j < N; j++) {
if (j != 0) printf(", ");
printf("%*d", width, a[i][j]);
}
printf("]\n");
}
return 0;
}
Output:
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]

Here you are.
#include <stdio.h>
int main(void)
{
enum { M = 3, N = 7 };
int a[M][N] =
{
{ 0, 232, 20, 96, 176, 0, 0 },
{ 0, 0, 24, 0, 0, 176, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }
};
int width = 4;
for ( size_t i = 0; i < M; i++ )
{
putchar( '[' );
for ( size_t j = 0; j < N; j++ )
{
if ( j != 0 ) putchar( ',' );
printf( "%*d", width, a[i][j] );
}
printf( "]\n" );
}
putchar( '\n' );
return 0;
}
The program output is
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]
You can change the value of the variable width if you want for example to output larger values than values consisting from 3 digits. That is the output is enough flexible.
If you want to place the array output in a separate function then the corresponding function can look the following way provided that the compiler supports variable length arrays.
#include <stdio.h>
void format_output( size_t m, size_t n, int a[m][n], int width )
{
for ( size_t i = 0; i < m; i++ )
{
putchar( '[' );
for ( size_t j = 0; j < n; j++ )
{
if ( j != 0 ) putchar( ',' );
printf( "%*d", width, a[i][j] );
}
printf( "]\n" );
}
}
int main(void)
{
enum { M = 3, N = 7 };
int a[M][N] =
{
{ 0, 232, 20, 96, 176, 0, 0 },
{ 0, 0, 24, 0, 0, 176, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }
};
int width = 4;
format_output( M, N, a, width );
putchar( '\n' );
return 0;
}
The program output is the same as shown above that is
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]

use this format of printf printf("%4d", arr[i][j]);
int main()
{
int arr[3][7] = { {0, 232, 20, 96, 176, 0, 0}
,{0, 0, 24, 0, 0, 176, 0}
,{0, 0, 0, 0, 0, 0, 0} };
for (int i = 0; i < 3; i++)
{
printf("[");
for (int j = 0; j < 7; j++)
{
if (j < 6)
printf("%4d,", arr[i][j]);
if (j == 6)
printf("%4d", arr[i][j]);
}
printf("]");
printf("\n");
}
}
PS:amount of space can be changed as needed ,with changing "%4d".

Related

How to import a material-ui slider with react in a web page?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Point Lighting</title>
<script src="https://unpkg.com/react#17/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom#17/umd/react-dom.development.js" crossorigin></script><script src="https://unpkg.com/#material-ui/core#4.11.0/umd/material-ui.production.min.js" crossorigin="anonymous"></script>
<style>
canvas {
width: 40vw;
height: 40vh;
background-color: lightpink;
}
#uiContainer {
top: 0;
width: 80%;
border: 2px solid blue;
background-color: lightblue;
padding: 10px;
float: left;
}
#ui {
background-color: coral;
transform: translate(0%, 0px);
display: block;
float: left;
}
</style>
<h3>
Drag slider to rotate
</h3>
</head>
<body>
<p>ok</p>
<canvas id="canvas" width="200" height="200"></canvas>
<div id="uiContainer">
<div id="ui">
</div>
</div>
<!-- vertex shader -->
<script id="3d-vertex-shader" type="x-shader/x-vertex">
attribute vec4 a_position;
attribute vec3 a_normal;
uniform vec3 u_lightWorldPosition;
uniform mat4 u_world;
uniform mat4 u_worldViewProjection;
uniform mat4 u_worldInverseTranspose;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
void main() {
// Multiply the position by the matrix.
gl_Position = u_worldViewProjection * a_position;
// orient the normals and pass to the fragment shader
// v_normal = mat3(u_worldInverseTranspose) * a_normal;
v_normal = mat3(u_world) * a_normal;
// compute the world position of the surfoace
vec3 surfaceWorldPosition = (u_world * a_position).xyz;
// compute the vector of the surface to the light
// and pass it to the fragment shader
v_surfaceToLight = u_lightWorldPosition - surfaceWorldPosition;
}
</script>
<!-- fragment shader -->
<script id="3d-fragment-shader" type="x-shader/x-fragment">
precision mediump float;
// Passed in from the vertex shader.
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
uniform vec4 u_color;
void main() {
// because v_normal is a varying it's interpolated
// so it will not be a unit vector. Normalizing it
// will make it a unit vector again
vec3 normal = normalize(v_normal);
vec3 surfaceToLightDirection = normalize(v_surfaceToLight);
float light = dot(normal, surfaceToLightDirection);
gl_FragColor = u_color;
// Lets multiply just the color portion (not the alpha)
// by the light
gl_FragColor.rgb *= light;
}
</script>
<script>
function RadToDegSlider(props) {
const {min, max, value, onChange} = props;
const [v, setV] = React.useState(radToDeg(value));
return (
<RangeSlider
min={min}
max={max}
value={v}
onChange={(e, v) => {
setV(v);
onChange(degToRad(v));
}}
/>
);
}
// Setup a ui.
const domContainer = document.querySelector('#ui');
ReactDOM.render(
<RadToDegSlider
value={getRotation()}
onChange={v => {
fRotationRadians = v;
drawScene();
}}
min={-360}
max={360} />, domContainer);
function getRotation() {
return fRotationRadians;
}
function updateRotation(value) {
fRotationRadians = value;
drawScene();
}
</script>
<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
<script src="https://webglfundamentals.org/webgl/resources/m4.js"></script>
<script>
"use strict";
function main() {
// Get A WebGL context
/** #type {HTMLCanvasElement} */
var canvas = document.getElementById("canvas");
var gl = canvas.getContext("webgl");
if (!gl) {
return;
}
// setup GLSL program
var program = webglUtils.createProgramFromScripts(gl, ["3d-vertex-shader", "3d-fragment-shader"]);
// look up where the vertex data needs to go.
var positionLocation = gl.getAttribLocation(program, "a_position");
var normalLocation = gl.getAttribLocation(program, "a_normal");
// lookup uniforms
var worldViewProjectionLocation = gl.getUniformLocation(program, "u_worldViewProjection");
var worldInverseTransposeLocation = gl.getUniformLocation(program, "u_worldInverseTranspose");
var colorLocation = gl.getUniformLocation(program, "u_color");
var lightWorldPositionLocation = gl.getUniformLocation(program, "u_lightWorldPosition");
var worldLocation = gl.getUniformLocation(program, "u_world");
// Create a buffer to put positions in
var positionBuffer = gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Put geometry data into buffer
setGeometry(gl);
// Create a buffer to put normals in
var normalBuffer = gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = normalBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
// Put normals data into buffer
setNormals(gl);
function radToDeg(r) {
return r * 180 / Math.PI;
}
function degToRad(d) {
return d * Math.PI / 180;
}
var fieldOfViewRadians = degToRad(50);
var fRotationRadians = 0;
drawScene();
// Draw the scene.
function drawScene() {
// webglUtils.resizeCanvasToDisplaySize(gl.canvas);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// console.log(gl.canvas.width,gl.canvas.height);
// Clear the canvas AND the depth buffer.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Turn on culling. By default backfacing triangles
// will be culled.
//gl.enable(gl.CULL_FACE);
// Enable the depth buffer
gl.enable(gl.DEPTH_TEST);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
// Turn on the position attribute
gl.enableVertexAttribArray(positionLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Tell the position attribute how to get data out of positionBuffer (ARRAY_BUFFER)
var size = 3; // 3 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var normalize = false; // don't normalize the data
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer( positionLocation, size, type, normalize, stride, offset);
// Turn on the normal attribute
gl.enableVertexAttribArray(normalLocation);
// Bind the normal buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
// Tell the attribute how to get data out of normalBuffer (ARRAY_BUFFER)
var size = 3; // 3 components per iteration
var type = gl.FLOAT; // the data is 32bit floating point values
var normalize = false; // normalize the data (convert from 0-255 to 0-1)
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer( normalLocation, size, type, normalize, stride, offset);
// Compute the projection matrix
var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
var zNear = 1;
var zFar = 2000;
var projectionMatrix = m4.perspective(fieldOfViewRadians, aspect, zNear, zFar);
// Compute the camera's matrix
var camera = [100, 150, 200];
var target = [0, 35, 0];
var up = [0, 1, 0];
var cameraMatrix = m4.lookAt(camera, target, up);
// Make a view matrix from the camera matrix.
var viewMatrix = m4.inverse(cameraMatrix);
// Compute a view projection matrix
var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
// Draw a F at the origin
var worldMatrix = m4.yRotation(fRotationRadians);
//console.log('stampa');
//console.log(worldMatrix);
// Multiply the matrices.
var worldViewProjectionMatrix = m4.multiply(viewProjectionMatrix, worldMatrix);
var worldInverseMatrix = m4.inverse(worldMatrix);
//console.log(worldInverseMatrix);
var worldInverseTransposeMatrix = m4.transpose(worldInverseMatrix);
//console.log(worldInverseTransposeMatrix);
// Set the matrices
gl.uniformMatrix4fv(worldViewProjectionLocation, false, worldViewProjectionMatrix);
gl.uniformMatrix4fv(worldInverseTransposeLocation, false, worldInverseTransposeMatrix);
gl.uniformMatrix4fv(worldLocation, false, worldMatrix);
// Set the color to use
gl.uniform4fv(colorLocation, [0.2, 1, 0.2, 1]); // green
// set the light position
gl.uniform3fv(lightWorldPositionLocation, [20, 30, 60]);
// Draw the geometry.
var primitiveType = gl.TRIANGLES;
var offset = 0;
var count = 16 * 6;
gl.drawArrays(primitiveType, offset, count);
}
}
// Fill the buffer with the values that define a letter 'F'.
function setGeometry(gl) {
var positions = new Float32Array([
// left column front
0, 0, 0,
0, 150, 0,
30, 0, 0,
0, 150, 0,
30, 150, 0,
30, 0, 0,
// top rung front
30, 0, 0,
30, 30, 0,
100, 0, 0,
30, 30, 0,
100, 30, 0,
100, 0, 0,
// middle rung front
30, 60, 0,
30, 90, 0,
67, 60, 0,
30, 90, 0,
67, 90, 0,
67, 60, 0,
// left column back
0, 0, 30,
30, 0, 30,
0, 150, 30,
0, 150, 30,
30, 0, 30,
30, 150, 30,
// top rung back
30, 0, 30,
100, 0, 30,
30, 30, 30,
30, 30, 30,
100, 0, 30,
100, 30, 30,
// middle rung back
30, 60, 30,
67, 60, 30,
30, 90, 30,
30, 90, 30,
67, 60, 30,
67, 90, 30,
// top
0, 0, 0,
100, 0, 0,
100, 0, 30,
0, 0, 0,
100, 0, 30,
0, 0, 30,
// top rung right
100, 0, 0,
100, 30, 0,
100, 30, 30,
100, 0, 0,
100, 30, 30,
100, 0, 30,
// under top rung
30, 30, 0,
30, 30, 30,
100, 30, 30,
30, 30, 0,
100, 30, 30,
100, 30, 0,
// between top rung and middle
30, 30, 0,
30, 60, 30,
30, 30, 30,
30, 30, 0,
30, 60, 0,
30, 60, 30,
// top of middle rung
30, 60, 0,
67, 60, 30,
30, 60, 30,
30, 60, 0,
67, 60, 0,
67, 60, 30,
// right of middle rung
67, 60, 0,
67, 90, 30,
67, 60, 30,
67, 60, 0,
67, 90, 0,
67, 90, 30,
// bottom of middle rung.
30, 90, 0,
30, 90, 30,
67, 90, 30,
30, 90, 0,
67, 90, 30,
67, 90, 0,
// right of bottom
30, 90, 0,
30, 150, 30,
30, 90, 30,
30, 90, 0,
30, 150, 0,
30, 150, 30,
// bottom
0, 150, 0,
0, 150, 30,
30, 150, 30,
0, 150, 0,
30, 150, 30,
30, 150, 0,
// left side
0, 0, 0,
0, 0, 30,
0, 150, 30,
0, 0, 0,
0, 150, 30,
0, 150, 0]);
// Center the F around the origin and Flip it around. We do this because
// we're in 3D now with and +Y is up where as before when we started with 2D
// we had +Y as down.
// We could do by changing all the values above but I'm lazy.
// We could also do it with a matrix at draw time but you should
// never do stuff at draw time if you can do it at init time.
var matrix = m4.xRotation(Math.PI);
matrix = m4.translate(matrix, -50, -75, -15);
for (var ii = 0; ii < positions.length; ii += 3) {
var vector = m4.transformPoint(matrix, [positions[ii + 0], positions[ii + 1],
positions[ii + 2], 1]);
positions[ii + 0] = vector[0];
positions[ii + 1] = vector[1];
positions[ii + 2] = vector[2];
}
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
}
function setNormals(gl) {
var normals = new Float32Array([
// left column front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
// top rung front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
// middle rung front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
// left column back
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
// top rung back
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
// middle rung back
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
// top
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
// top rung right
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// under top rung
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
// between top rung and middle
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// top of middle rung
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
// right of middle rung
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// bottom of middle rung.
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
// right of bottom
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// bottom
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
// left side
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0]);
gl.bufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW);
}
main();
</script>
</body>
</html>
I have all the code in this html file and I import the libraries with cdn, but I can't see the slider in the web page. I'm new to react and webgl, and don't understand where the problem is. How can I do? Thank you all.
There are few issues in your code, I've created a working demo. https://codesandbox.io/s/eager-rgb-9h18u?file=/index.html
You have missed babel import
Missing type="text/babel" on script tag
Slider is the correct component name

how to read multiple lines from serial C, terminated by carriage return AND line feed?

I am working on a project where I need to communicate from an AVR microcontroller to an rs232 device via UART and a TTL level converter. I am using a serial callback to get the characters from the serial port, and checking for a carriage return/new line, then also checking the most recent character in the buffer for carriage return/new line. The data from the rs232 device is coming in the format of 26 lines seperated by carriage return AND line feed comprised of ASCII characters. The entire 26 lines is sent approximately every .7 seconds. I am trying to fill a 2D array with the 26 lines. However, I am now getting a what I should in the receive buffer. Here is my code so far:
volatile uint8_t rxrs_buffer[26][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
static void serialrs_rx_cb(const struct usart_async_descriptor *const io_descriptor)
{
//counters
uint8_t ch, count;
//read a character
count = io_read(&SERIALRS232.io, &ch, 1);
//check if we are receiving
if (serialrs_receiving == 0)
{
//check for new line or carriage return
if (ch != 10 && ch != 13)
{
if (rxrs_buffer[row-1][column-1] != 10 && rxrs_buffer[row-1][column-1] != 13)
{ //set receiving flag
serialrs_receiving = 1;
//reset byte counter
serialrs_bytes_received_counter = 0;
//start filling the rx buffer
rxrs_buffer[row][column] = ch;
//increment the byte counter and column
serialrs_bytes_received_counter += count;
column++;
}
}
}
else
{
//continue filling the buffer
rxrs_buffer[row][column] = ch;
//increment the byte counter and column
serialrs_bytes_received_counter += count;
column++;
//check for new line or carriage return
if (ch == 10 || ch == 13)
{
if (rxrs_buffer[row-1][column-1] != 10 && rxrs_buffer[row-1][column-1] != 13)
{
//continue filling the buffer
rxrs_buffer[row][column] = ch;
//increment the byte counter
serialrs_bytes_received_counter += count;
//increment the line counter and reset column
row++;
column = 0;
if (row == 26)
{
//set the completion flag
serialrs_complete = 1;
//total bytes
totalrs_bytes = serialrs_bytes_received_counter - 2;
row = 0;
}
}
}
//check for buffer overflow
if (serialrs_bytes_received_counter >= SERIALRS_BUFFER_SIZE)
{
//reset buffer counter
serialrs_bytes_received_counter = 0;
}
}
}
The issue is my 2D buffer array is not filling up correctly. I am getting rows that start with two line feeds, then some that work perfectly and end in a carriage return line feed, only to find the next row starts with two carriage returns or line feeds? Any suggestions at all would help

how to extract data, convert it to ascii and push it in a list in javascript

I receive data from an external device in decimal values, that I need to convert in ascii and then push it in a list
data example:
#onData: 1,3,200,
78,69,84,71,69,65,82,45,71,117,101,115,116,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70,114,105,103,111,109,97,116,45,49,50,51,52,53,54,55,56,45,87,73,70,73,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,45,71,85,69,83,84,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,123
The first 3 elements of #onData need to be ignored and the last 2 elements as well.
In ascii it would be:
NETGEAR-Guest
Frigomat-12345678-WIFI
MICRO-SYSTEMS
MICRO-SYSTEMS-GUEST
MICRO-SYSTEMS
I have a method to convert:
arrayBuffer2str(buf) {
var str = "";
var ui8 = new Uint8Array(buf);
for (var i = 0; i < ui8.length; i++) {
str = str + String.fromCharCode(ui8[i]);
}
return str;
}
I would need to get rid of the zeros and then push it in an array of strings:
this.wifiNetworks.push("Network-1");
Thank you
If you want to extract the buffer between two zeros and isolate the significant response, try this
let prevVal;
let phrase= [];
for (let i=0; i<array.length; i++) {
let val = array[i];
if (val == 0 && prevVal != 0) {
// end of response
process(phrase);
}
if (prevVal == 0 && val != 0) {
// start new response
phrase= [];
}
phrase.push(val);
prevVal = val;
}
function process(phrase) {
// TODO Process your phrase here and remove the inner zeros
}
Also, consider removing your zeros using the .filter function
Try this,
Assuming that we are getting the input as an array.
const inputArray = [1, 3, 200,
78, 69, 84, 71, 69, 65, 82, 45, 71, 117, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70, 114, 105, 103, 111, 109, 97, 116, 45, 49, 50, 51, 52, 53, 54, 55, 56, 45, 87, 73, 70, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 45, 71, 85, 69, 83, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 123];
const resultArray = [];
let stringIndex = 0;
for (i = 3; i <= inputArray.length - 3; i++) {
if (inputArray[i] !== 0) {
resultArray[stringIndex] = resultArray[stringIndex]
? resultArray[stringIndex].concat(String.fromCharCode(inputArray[i]))
: ''.concat(String.fromCharCode(inputArray[i]));
} else if (inputArray[i - 1] !== 0) {
stringIndex = stringIndex + 1
}
}
console.log(resultArray);
// Add code to push the result to server,
The output would be
[ 'NETGEAR-Guest', 'Frigomat-12345678-WIFI', 'MICRO-SYSTEMS',
'MICRO-SYSTEMS-GUEST', 'MICRO-SYSTEMS' ]

Printing a two dimensional array in C

I want to print a two dimensional array which I get from another method, see code:
int** getPrint(){
const int EIGHT[7][5] = {
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main(){
int **ptr;
ptr = getPrint();
return 0;
}
What would be the best method to print this?
Something as the following
#include <stdio.h>
#define N 7
#define M 5
const int ( * getPrint( void ) )[M]
{
static const int EIGHT[N][M] =
{
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main( void )
{
const int ( *ptr )[M];
int i, j;
ptr = getPrint();
for ( i = 0; i < N; i++ )
{
for ( j = 0; j < M; j++ ) printf( "%d ", ptr[i][j] );
printf( "\n" );
}
return 0;
}
Or you could use a typedef. For example
#include <stdio.h>
#define N 7
#define M 5
typedef const int ( *ArrayPtr )[M];
ArrayPtr getPrint( void )
{
static const int EIGHT[N][M] =
{
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main( void )
{
ArrayPtr ptr;
int i, j;
ptr = getPrint();
for ( i = 0; i < N; i++ )
{
for ( j = 0; j < M; j++ ) printf( "%d ", ptr[i][j] );
printf( "\n" );
}
return 0;
}

How to tag taken seats in Cinema. C Programming

First I want to say that I am a beginner in C and in programming at all.
C is my first language and I find it very interesting.
I am writing a program that will simulate a cinema software. I mean you choose movie, time and you choose seats.
I am done with selecting the movie and time but I have a problem with the seats.
The thing I am trying to do is when you select Row and Column, somehow to print out in the console which seat is taken (changing the color, increasing the font or something like that)
Here is my code:
void SeatSelection()
{
int row = 0;
int column = 0;
int i, j;
printf("\t\t\t\tSCREEN\n\n\n\n\n\n\n");
int A[11][11] = {
{ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
};
for (i = 0; i < 10; i++)
{
for (j = 0; j < 11; j++)
printf("%d\t", A[i][j]);
printf("\n\n");
}
do
{
printf("Choose seat: Row and Column\n");
scanf("%d %d", &row, &column);
if ((row<1 || row>10) && (column<1 || column>10)) printf("Wrong choice, try again\n");
} while ((row<1 || row>10) && (column<1 || column>10));
}
Thanks for the help in advance :)
Try using a second table taken[][]. For a seat (row, column) taken[row][column] is 1 if the seat is taken, else it is 0. Code:
#include <stdio.h>
void SeatSelection()
{
int row = 0;
int column = 0;
int i, j;
int A[11][11] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
};
int taken[11][11] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
printf("============= The Cinema ==============\n");
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++)
printf(" %d ", A[i][j]);
printf("\n");
}
fflush(stdout);
do
{
printf("Choose seat: Row and Column\n");
fflush(stdout);
scanf("%d %d", &row, &column);
if ((row<1 || row>10) && (column<1 || column>10)) printf("Wrong choice, try again\n");
if(taken[row][column]) printf("This seat is taken, try again\n");
else {
taken[row][column] = 1;
printf("======== The Cinema =========\n");
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++) {
if(taken[i][j] == 0)
printf(" %d ", A[i][j]);
else
printf("[%d] ", A[i][j]);
}
printf("\n");
}
}
fflush(stdout);
} while (true);
}
int main() {
SeatSelection();
return 0;
}
Edit
I have made some alterations to your code, but i think you get the idea :) If you don't understand something just tell me...

Resources