Print Odd numbers using 2 arrays in a function - c

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++;
}
}
}

Related

why does the output varies for same input

#include <stdio.h>
void fun(int *, int size, int *, int *new_size);
int main()
{
int size, new_size; // variables declaration
printf("Enter the size : "); // prompting user to enter the size of the array
scanf("%d", &size); // reading size from the user
int arr[size], arr1[size]; // declaring array of size
printf("Enter the elements into the array: "); // prompt the user to enter the elements of array
for (int i = 0; i < size; i++)
{
scanf("%d", arr + i); // reading the elements of array from user
}
fun(arr, size, arr1, &new_size); // calling function
printf("After removing duplicates: "); // prompting user a new array after removing duplicates
for (int i = 0; i < new_size; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
printf("before removing duplicates: ");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void fun(int *arr, int size, int *arr1, int *new_size) // function definition
{
int count = 1; // declaring variables
arr1[0] = arr[0];
for (int i = 0; i < size; i++)
{
int flag = 0;
for (int j = 0; j < i; j++)
{
if (arr[i] == arr1[j])
{
break;
}
flag = flag + 1;
if (arr1[flag] == 0)
{
*(arr1+flag) = arr[i];
count = count + 1;
}
}
}
*new_size = count;
}
basically what i am doing is, getting the array elements and removing duplicates elements and assigning non-duplicate elements to new array,while without modifying the old array and displaying new array with modification, old array without modification.
But the problem is when i execute this code the output varies for same input values. Why is that i couldn't figure it out.
Here is the output of the program.
Enter the size : 5
Enter the elements into the array: 5
1
2
1
2
After removing duplicates: 5 1 2
before removing duplicates: 5 1 2 1 2
Enter the size : 5
Enter the elements into the array: 5
4
3
2
1
After removing duplicates: 5 4 3 2 1
before removing duplicates: 5 4 3 2 1
Enter the size : 5
Enter the elements into the array: 5
1
2
1
2
After removing duplicates: 5 1 6
before removing duplicates: 5 1 2 1 2
see the output changes

How to print user input array in one line

Trying to print out the user input of the array online but ended up in prints in one element at a time.
The following code aims to compute the sum of elements in the n number of ArrayList:
// let int count be counter
int count=0;
int inputNum;
// calculate the length of the array
int len;
for (int i=0; i<numOfLines; i++)
{
count++;
printf("Enter line %d: \n", count);
for (int j=0; j<numOfLines; j++)
{
scanf("%d", &inputNum);
printf("DEBUG:input number %d \n", inputNum++);
if (inputNum != 0)
{
int arrNum[]= {inputNum++};
len = sizeof(arrNum)/sizeof(arrNum[0]);
printf("Total: %d \n", len);
}
}
}
Output:
Enter number of lines:
2
Enter line 1:
3 2 3 4
DEBUG:input number 3
Total: 1
DEBUG:input number 2
Total: 1
Enter line 2:
DEBUG:input number 3
Total: 1
DEBUG:input number 4
Total: 1
Correct sample output:
Enter number of lines:
2
Enter line 1:
3 2 3 4
Total: 9
Enter line 2:
4 1 2 3 4
Total: 10
It seems the first number of each line is the number of data in that line. Use that information.
// let int count be counter
int count=0;
int inputNum;
for (int i=0; i<numOfLines; i++)
{
count++;
printf("Enter line %d: \n", count);
int sum = 0;
scanf("%d", &inputNum);
for (int j=0; j<inputNum; j++)
{
int value;
scanf("%d", &value);
printf("DEBUG:input number %d \n", value);
sum += value;
}
if (inputNum != 0)
{
printf("Total: %d \n", sum);
}
}

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++;
}

How to printf a list of values?

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]);
}
}

Resources