How to printf a list of values? - c

When I Enter new values they should display in View as a list like
1. 1 2 3 4 5 //first values
2. 6 5 9 8 3 // second values
3. 4 2 3 8 5 // ...
but instead it prints just the first values to the whole list. I try to fix this wihout a 2d array if possible. Any ideas?
#include <stdio.h>
#define LENGTH 5
const char *menuMsg = "\n\n\t Menu \n\n\t v (View)\n\t e (Enter)\n\t q (Quit)\n";
int main(){
int run = 1;
while(run){
puts(menuMsg);
char choice;
scanf(" %c", &choice);
int x, z;
int a[LENGTH];
int list=6;
if(choice=='e') {
for(x=0; x<LENGTH; x++){
printf("Enter nr.%d: ", x+1);
scanf("%d", &a[x]);
}
z++;
}
else if(choice=='v') {
for(z=0; z<list; z++){
printf("\n%d. ", z+1);
for(x=0; x<LENGTH; x++){
printf("%d ", a[x]);
}
}
}
else if(choice=='q') run = 0;
}
return 0;
}

The length of your array a is only 5. I'm concerned about where you define it since you expect it to print out 6 rows of 5 values- 30 individual numbers.
This might work, modify to your needs:
#define LENGTH 5 // amount in a row (number of cols)
#define HEIGHT 6 // amount of rows
a[LENGTH * HEIGHT] = { 0, 2, 3, 4...};
for (z=0; z < HEIGHT; z++){
printf("\n%d. ", z+1);
for (x=0; x < LENGTH; x++){
printf("%d ", a[x + LENGTH*z]);
}
}

Related

Print Odd numbers using 2 arrays in a function

The point of my code is to print all the odd numbers inputted each array using function.
#include<stdio.h>
void printOdd(int arr1[], int arr2[], int s1, int s2) {
int count = 0;
for (int i = 0; i < s1; i++) {
if (arr1[i] % 2 != 0) {
printf("%d", arr1[i]);
count++;
if(count<(s1+s2)/2)
{
printf(", ");
}
}
}
for (int i = 0; i < s2; i++) {
if (arr2[i] % 2 != 0) {
printf("%d", arr2[i]);
if(count<(s1+s2)/2-1)
{
printf(", ");
}
}
}
}
int main(void)
{
int s1, s2;
printf("Enter first array size: ");
scanf("%d", &s1);
int arr1[s1];
printf("Enter second array size: ");
scanf("%d", &s2);
int arr2[s2];
printf("Enter first array values: ");
for(int x = 0; x < s1; x++)
{
scanf("%d", &arr1[x]);
}
printf("Enter second array values: ");
for(int y = 0; y < s2; y++)
{
scanf("%d", &arr2[y]);
}
printOdd(arr1, arr2, s1, s2);
}
These are the expected outputs
Enter first array size: 5
Enter second array size: 5
Enter first array values: 1 2 3 4 5
Enter second array values: 6 7 8 9 10
1, 3, 5, 7, 9
Enter first array size: 3
Enter second array size: 3
Enter first array values: 5 6 10
Enter second array values: 12 41 36
5, 41
Enter first array size: 4
Enter second array size: 3
Enter first array values: 1 2 3 5
Enter second array values: 3 2 1
1, 3, 5, 3, 1
My problem is everytime I input numbers, the result is that there is always a comma at the end, here's my output
Enter first array size: 3
Enter second array size: 3
Enter first array values: 5 6 10
Enter second array values: 12 41 36
5, 41,
https://gyazo.com/9d544951a3572666a3b6b0dc8620d9cc
the link is a picture of expected output and my output
The simplest thing to do is to print the comma before printing your value, and only when count is non-zero. The reason is that you never know if there will be a next odd number, but you do know when there was a previous one. So you should always leave your output with no trailing comma.
void printOdd(int arr1[], int arr2[], int s1, int s2) {
int count = 0;
for (int i = 0; i < s1; i++) {
if (arr1[i] % 2 != 0) {
if (count > 0) printf(", ");
printf("%d", arr1[i]);
count++;
}
}
for (int i = 0; i < s2; i++) {
if (arr2[i] % 2 != 0) {
if (count > 0) printf(", ");
printf("%d", arr2[i]);
count++;
}
}
}

Why just looping the last input?

so I wanna make a program to find each sum of the columns of a matrix. In this code first you can input how many matrix you want and the second input is the size of matrix (n x n) and third input is the value of matrix.
The output must be each sum of the columns of matrix
here is the code
#include <stdio.h>
int main ()
{
int x, y, z;
int inp1, inp2;
int matrix[100][100];
int matrix2;
scanf("%d", &inp1);
for(z = 0; z < inp1; z++){
scanf("%d", &inp2);
for(x = 0; x < inp2; x++){
for(y = 0; y < inp2; y++){
scanf("%d", &matrix[x][y]);
}
}
}
for(z = 0; z < inp1; z++){
printf("Case #%d:", z+1);
for(x = 0; x < inp2; x++){
matrix2 = 0;
for(y = 0; y < inp2; y++){
matrix2 += matrix[y][x];
if(y == inp2-1){
printf(" %d", matrix2);
}
}
}
printf("\n");
}
return 0;
}
here's the input and ouput for that code
2 //first input (how many matrix)
2 //second input (size of matrix)
1 2 //third input (matrix)
1 2
3
1 1 1
2 2 2
3 4 5
Case #1: 6 7 8
Case #2: 6 7 8
for the second output is correct but the first output is wrong.
output for the second one from 1+2+3 = 6, 1+2+4 = 7, 1+2+5 = 8
What I want is the output for the first case is 2 and 4

How to append values to an array in C and calculating the shortest distance between two points

I need help on making a program that will count the shortest distance between two places that are arranged in a circular pattern I need to count the distance between first and first, first and second and so on keeping in mind I can go the other way around if the distance is shorter and place the results in a matrix equal to the length of the array that we have inputted at the beginning of the program, so the output should look something like this:
input:
Array length=5
Array elements:2 3 4 1 1
so the output should look like this:
2 3 4 1 1
0 2 5 2 1
2 0 3 4 3
5 3 0 4 5
2 4 4 0 1
1 3 5 1 0
The code that I wrote only gets user input for array length and elements and outputs the first line which is the elements of an array that we have gotten from the user input. So my question is how to get that shortest way for every distance and append those values to the matrix.
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int l,n, i, m, x,y,s,j,t;
int **p;
scanf("%d", &l);
ptr = (int*)malloc(l * sizeof(int));
if (ptr == NULL) {
exit(0);
}
else {
for (i = 0; i < l; ++i)
{
scanf("%d", &ptr[i]);
if(ptr[i]<0)
{
exit(0);
}
}
for (i = 0; i < l; ++i) {
printf("%d ", ptr[i]);
}
}
n=l;
m=l;
while(m>0 && n>0){
p= malloc(m*sizeof(int*));
for (i=0;i<m;i++){
p[i]=malloc(n*sizeof(int));
for(j=0;j<n;j++){
x=0;
y=0;
for (t = 0; t < j; t++) {
x = +ptr[t];
}
for (t = n; t >j; t--) {
y = +ptr[t];
}
if (x > y) {
s = y;
} else {
s = x;
}
p[i][j]=s;
}
}
}
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
printf("%d", p[i][j]);
}
putchar('\n');
}
return 0;
}

How to read tetrads (groups of four integers) and save it in 2d array?

This is the function that I have written:
int read_data(int (*p)[0][0])
{
int i,j;
for (i=0;i<N;i++)
{
for (j=0;j<1;j++)
{
printf("give first number:");
scanf("%d",&(*p)[i][j]);
printf("give second number:");
scanf("%d",&(*p)[i][j]);
printf("give third number:");
scanf("%d",&(*p)[i][j]);
printf("give forth number:");
scanf("%d",&(*p)[i][j]);
}
}
for (i=0;i<N;i++)
{
for (j=0;j<4;j++)
{
printf("%d [%d],[%d]\n",(*p)[i][j],i,j);
printf("%d [%d],[%d]\n",(*p)[i][j],i,j);
printf("%d [%d],[%d]\n",(*p)[i][j],i,j);
printf("%d [%d],[%d]\n",(*p)[i][j],i,j);
}
}
}
I have to solve an exercise. One part of the problem is to read tetrads of integers (groups of four integers) and save it in an array. I tried many different approaches, but none seem to work.
Thanks in advance.
Here is a very simple solution.
Numbers are assigned directly in the 2 dimension array in a single scanf (you can use indices but it is not really needed because you know you can have only 4 numbers).
The function scanf_tetrad takes the array as parameter.
Code:
#include <stdio.h>
#include <stdlib.h>
void scanf_tetrad(int p[2][2])
{
printf("Enter 4 numbers:");
scanf("%d %d %d %d", &p[0][0], &p[0][1], &p[1][0], &p[1][1]);
}
int main()
{
int ia[2][2];
int i,j;
scanf_tetrad(ia);
printf("You entered: ");
for (i=0; i < 2; i++)
for(j=0; j < 2; j++)
printf("%d ", ia[i][j]);
printf("\n");
return 0;
}
Execution:
./tetrad
Enter 4 numbers:54634 456 7486743 4546
You entered: 54634 456 7486743 4546
Here is simple program to read multiple tetrads:
it firsts asks to enter the number of tetrad
it allocates a dynamic array tt which in array of tetrad arrays
it allocates each array tetrad in tt
it reads each tetrad array with the same above function
it displays the tetrad array items using a formula to access array item address [x,y] which is x * (number of columns) + y. Note that this code may not be 100% C standard compliant.
metrad.c
#include <stdio.h>
#include <stdlib.h>
void scanf_tetrad(int p[2][2])
{
printf("Enter 4 numbers:");
scanf("%d %d %d %d", &p[0][0], &p[0][1], &p[1][0], &p[1][1]);
}
int main()
{
int i,j, idx;
int ttnb;
int ***tt, **ctt;
printf("Enter number of tetrad:");
scanf("%d", &ttnb);
tt = malloc(ttnb * sizeof (int [2][2]));
if (tt == NULL)
{
perror("malloc");
return 1;
}
for (idx=0; idx < ttnb; idx++)
{
tt[idx] = malloc(sizeof (int [2][2]));
if (tt[idx] == NULL)
{
perror("malloc");
return 1;
}
}
for (idx=0; idx < ttnb; idx++)
{
scanf_tetrad((int (*)[2])tt[idx]);
}
printf("You entered:\n");
for (idx=0; idx < ttnb; idx++)
{
printf("tetrad %d: ", idx);
ctt = tt[idx];
for (i=0; i < 2; i++)
for(j=0; j < 2; j++)
{
printf("%d ", *((int*)ctt + (i * 2) + j));
}
printf("\n");
}
return 0;
}
Example of execution:
./mtetrad
Enter number of tetrad:4
Enter 4 numbers: 1 2 3 4
Enter 4 numbers:5 6 7 8
Enter 4 numbers:9 10 11 12
Enter 4 numbers:13 14 15 16
You entered:
tetrad 0: 1 2 3 4
tetrad 1: 5 6 7 8
tetrad 2: 9 10 11 12
tetrad 3: 13 14 15 16

How to print this series?(1 \n 2 3 \n 4 5 6 \n 7 8 9 10... ... ...)

I am trying to print the following series: 1 2 3 4 5 6 7 8 9 10 ... ... ...The input of my program contains a single integer n, which determines the number of lines to print.
I've tried to code it but got the following output:
12 33 4 54 5 6 7... ... ...
#include<stdio.h>
int main()
{
int n,i,j,t,m;
scanf("%d", &n);
for(i=1;i<=n;i++)
{
for(j=i,t=1;t<=i;j++,t++)
{
printf("%d ",j);
}
printf("\n");
}
}
To print those numbers, you'll want a counter that starts at 1, increases by 1 every print and is never reset by anything. Adjust your loop like this:
int main()
{
int n, i, j, t = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++, t++)
{
printf("%d ", t);
}
printf("\n");
}
}
Note how t is set to 1, and just gets increased by t++ without resetting like you previously did. Also, you should be printing t, not j.
You should maintain separate counters for the numbers and the number of numbers per line.
int nr = 1, target;
int nrsperline = 1, i;
scanf("%d", &target);
while (nr <= target) {
for (i = 0; i < nrsperline; i++) {
printf("%d ", nr++);
}
printf("\n");
nrsperline++;
}

Resources