I have to print a triangle of integers of collatz in c - c

I have to write a program in C which, prints numbers from collatz as a triangle.
Like this:
I have tried and
This is my output:
This is my code:
int inputLines, startNum, number;
printf("Lines: ");
scanf("%d",& inputLines);
printf("Start: ");
scanf("%d",& startNum);
for(int i = 0; i < inputLines; i++){
printf("\n");
for(int j = 0; j <= i; j++){
printf("%d ", startNum);
if(startNum % 2 == 0){
startNum = startNum / 2;
}else{
startNum = startNum * 3 + 1;
}
}
}

#include <stdio.h>
int main(void) {
int inputLines = 4;
int start = 19;
for(int i = 0; i < inputLines; i++)
{
int n = start;
for(int j=0; j<i+1; ++j)
{
printf("%d ", n);
n = (n%2)? 3*n+1 : n/2;
}
printf("\n");
}
return 0;
}

Related

Can anyone tell me the problem with my code?

Everythings right except the output of the last part, I'm supposed to get this output https://i.stack.imgur.com/PRJMT.png but instead it got this https://i.stack.imgur.com/iZflq.png
Also heres my code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of goal-scorers: ");
scanf("%d", &n);
int ar[n];
for(int i=0; i<n; i++){
printf("Score of player #%d: ", i+1);
scanf("%d", &ar[i]);
}
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(ar[i] <ar[j]){
int temp = ar[j];
ar[j] = ar[i];
ar[i]; temp;
}
}
}
printf("\nHighest to lowest:\n");
for(int i=0; i<n; i++)
printf("Player #%d: %d\n",i+1, ar[i]);
return;
}
Found it!
ar[i]; temp;
But you want ar[i] = temp;
You enabled compiler warnings. What I don't get is why the warning for "statement has no effect" is not showing up.
The possible solution may be like :
#include <stdio.h>
struct player {
int no;
int score;
};
int main() {
int n;
printf("Enter the number of goal-scorers: ");
fflush(stdout);
scanf("%d", &n);
if (n <= 0)
return 0 ;
struct player players[n];
for (int i = 0 ; i < n ; i++) {
players[i].no = 1 + i;
printf("Score of player #%d: ", players[i].no);
fflush(stdout);
scanf("%d", &players[i].score);
}
for (int i = 0 ; i < n ; i++) {
for (int j = i + 1 ; j < n ; j++) {
if (players[i].score < players[j].score) {
struct player temp = players[j];
players[j] = players[i];
players[i] = temp;
}
}
}
printf("\nHighest to lowest:\n");
for (int i = 0 ; i < n ; i++)
printf("Player #%d: %d\n", players[i].no, players[i].score);
return 0;
}

Finding the highest frequency of an array and all elements which have that frequency

I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?
Format Input:
The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.
Format Output:
Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.
Constraints:
1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5
Sample Input:
3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
4
1 1 1 3
Sample Output:
Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1
Here is my code:
#include <stdio.h>
int main() {
int T, N[20];
scanf("%d", &T); getchar();
int A[T][20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N[i]); getchar();
for (int j = 0; j<N[i]; j++) {
scanf("%d", &A[i][j]); getchar();
}
int X = 0;
for (int j = 0; j<N[i]; j++) {
for (int k = j + 1; k<N[i]; k++) {
if (A[i][k]<A[i][j]) {
X = A[i][j];
A[i][j] = A[i][k];
A[i][k] = X;
}
}
}
}
int f[20000];
for (int i = 0; i<T; i++) {
int c = 0, mc = 0;
for (int j = 0; j<N[i]; j++) {
c = 1;
if(A[i][j] != -1) {
for (int k = j+1; k<N[i]; k++) {
if (A[i][j] == A[i][k]) {
c++;
A[i][k] = -1;
}
}
f[j]=c;
}
if (c>mc) {
mc = c;
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N[i]; j++) {
if (A[i][j] != -1) {
if (f[j] == mc) {
printf ("%d", A[i][j]);
if (j<N[i]-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
EDIT
So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.
NEW CODE
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T); getchar();
int A[20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
for (int j = 0; j<N; j++) {
scanf("%d", &A[j]); getchar();
}
int X;
for (int j = 0; j<N; j++) {
for (int k = j + 1; k<N; k++) {
if (A[k]<A[j]) {
X = A[j];
A[j] = A[k];
A[k] = X;
}
}
}
int f[N], c = 0, mc = 0;
for (int j = 0; j<N; j++) {
c = 1;
if(A[j] != -1) {
for (int k = j+1; k<N; k++) {
if (A[j] == A[k]) {
c++;
A[k] = -1;
}
}
f[j]=c;
if (c>mc) {
mc = c;
}
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N; j++) {
if (A[j] != -1) {
if (f[j] == mc) {
printf ("%d", A[j]);
if (j<N-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T);
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
//INPUT elements and counting frequncy for each element
int f[200001] = {0}, E = 0;
for (int j = 0; j<N; j++) {
scanf("%d", &E); getchar();
f[E]++;
}
//find max frequency and how many elements with max frequency
int max = 0, c = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
c ++;
}
if (f[j]>max) {
max = f[j];
c = 1;
}
}
//OUTPUT result
printf("Case #%d: %d\n", i+1, max);
int counter = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
counter ++;
if (counter<c){
printf("%d ", j);
} else {
printf("%d\n", j);
}
}
}
}
return 0;
}

Why can;t I printf the 2-D array by array[][] format?

I am practicing my C program currently and I want to calculate two 2D matrix.
But I get confused when I use printf() to check these arrays. They break the program.
Code as below:
int matric_multi_main()
{
int m1_row = 0;
int m1_col = 0;
int m2_row = 0;
int m2_col = 0;
int **matric1;
int **matric2;
printf("Please enter Matric 1 row: ");
scanf("%d", &m1_row);
printf("Please enter Matric 1 column: ");
scanf("%d", &m1_col);
printf("Please enter Matric 2 row: ");
scanf("%d", &m2_row);
printf("Please enter Matric 2 column: ");
scanf("%d", &m2_col);
if (m1_col != m2_row)
{
printf("Error matric size!!!\n");
return 0;
}
/* Allovate memory for matric 1 */
matric1 = malloc(m1_row * sizeof(int *));
for (int i = 0; i < m1_row; i++)
{
matric1[i] = malloc(m1_col * sizeof(int));
}
/* Allovate memory for matric 2 */
matric2 = malloc(m2_row * sizeof(int *));
for (int i = 0; i < m2_row; i++)
{
matric2[i] = malloc(m2_col * sizeof(int));
}
/* Init matric 1 */
for (int i = 0; i < m1_row; i++)
{
for (int j = 0; j < m1_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric1+i*m1_row+j);
}
}
/* Init matric 2 */
for (int i = 0; i < m2_row; i++)
{
for (int j = 0; j < m2_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric2+i*m2_row+j);
}
}
for (int i = 0; i < m1_row; i++)
{
printf("[");
for (int j = 0; j < m1_col; j++)
{
//printf("%d ", *(matric1 + i*m1_row + j)); <- correctly
//printf("%d ", &matric1[i][j]); <-- display the data with wrong order
printf("%d ", matric1[j][i]); <- break the program
}
printf("]\n");
}
for (int i = 0; i < m2_row; i++)
{
printf("[");
for (int j = 0; j < m2_col; j++)
{
//printf("%d ", *(matric2 + i*m2_row + j));
printf("%d ", matric2[i][j]);
}
printf("]\n");
}
}
I think I can use matric1[i][j] to get the correct data directly.
But the example on the website always use array[][] directly.
I can understand the different between my program and example.

In C program which remove duplicates from an array, when I print an array, the output is not true

This program which written in C should remove duplicated elements from 2 input arrays by user, so when I print the second array which is b[z] after removing duplicates, the output is not true as it prints weird number instead of the input number by the user. (the problem in code is determined by comment).
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, z ;
printf("Enter size of array\n");
scanf("%d", &n);
int a[n];
if(n <= 20) {
for(int i = 0 ; i < n; i++) {
printf("Enter integer \n");
scanf("%d", &a[i]);
}
}
for(int i = 0 ; i < n; i++) {
printf("%d ", a[i]);
}
printf("\nEnter size of the 2nd array\n");
scanf("%d", &z);
int b[z];
if(z <= 20) {
for(int i = 0 ; i < z; i++) {
printf("Enter integer \n");
scanf("%d", &b[z]);
}
}
for(int i = 0 ; i < z; i++) {
printf("%d ", b[z]);
}
for(int i = 0 ; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[i] == a[j]) {
for(int l = j; l < n; l++)
{
a[l] = a[l + 1];
}
n--;
j--;
}
}
}
printf("\nArray1: ");
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
for(int t = 0; t < z; t++) {
for(int u = t + 1; u < z; u++) {
if(b[t] == b[u]) {
for(int l = u; l < z; l++)
{
b[l] = b[l + 1];
}
z--;
u--;
}
}
}
printf("\nArray2: ");
for(int e = 0; e < z; e++) {
printf("%d ", b[e]);
}
return 0;
}

how to make a christmas tree using loop in c program

I'm a freshmen student and we have an activity in intro pro.. We were tasked to create a Christmas tree using a loop...
I have my code here:
#include<stdio.h>
int main ()
{
int rows,a,b,space;
clrscr();
printf("Enter a number of rows:");
scanf("%d",&rows);
space=rows-1
for(b=space;b>=1;b--)
{
for(a=rows;a>=1;a--)
space--;
printf("");
for(a=2*(rows-b)-1;a>=1;a--)
printf("*",a);
printf("\n");
space = space-1;
}
getche();
return 0;
}
This code was given to us by our professor... the program runs, but the output is wrong. Can you help me?
when i run this program, the output was like this:
*
***
*****
******
*******
You have to find a pattern. Say you want a tree with n rows. Last row is going to have 2n-1 stars. Row before it will have 2n-3 and so on. To print a row, first you print a number of spaces, then a number of stars. For last row, you print 0 spaces and 2n-1 stars. For row before it, you print 1 space and 2n-3 stars and so on.
for(int i = 0; i < n; i++)
{ for(int j = i + 1; j < n; j++)
printf(" ");
for(int j = 0; j <= 2*i; j++)
printf("*");
if(i < n - 1) puts("");
}
The Code is a little bit to messed up for me, but this should work:
#include<stdio.h>
int main() {
/*Variables*/
int rows, starNumber, spaceNumber;
int rowCount, spaceCount, starCount, treeTrunkCount, treeTrunkSpaceCount;
printf("Enter Rows:\n>");
scanf("%d",&rows);
for(rowCount = 1; rowCount <= rows; rowCount++) {
starNumber = rowCount * 2 - 1;
spaceNumber = rowCount + rows - starNumber;
for(spaceCount = 0; spaceCount < spaceNumber; spaceCount++)
printf(" ");
for(starCount = 0; starCount < starNumber; starCount++)
printf("%c",'*');
printf("\n");
}
for(treeTrunkCount = 0; treeTrunkCount < 3; treeTrunkCount++) {
for(treeTrunkSpaceCount = 0; treeTrunkSpaceCount < (rows * 2 + 1)/2; treeTrunkSpaceCount++)
printf(" ");
printf("%c\n",'*');
}
}
This is the simplest solution to your program..
#include <stdio.h>
int main()
{
int i=-1,j=0,rows;
printf("Enter Rows:\n");
scanf("%d",&rows);
while(j++<rows) // Moving pointer for the first '*'
{
printf(" ");
}
printf("*"); // This prints the first '*'
while(++i<rows)
{
for(j=-2;++j<rows-i;) // This loop will print Spaces before '*' on each row
printf(" ");
for(j=0;++j<2*i;) // This loop will print * on each row
{
printf("*");
}
printf("\n"); // This printf will take you to the next Line
}
}
This is the shortest and simplest solution for your question:
#include<stdio.h>
#include<conio.h>
void main(){
int count;
int i,j;
printf("enter the numbers of line");
scanf("%d",&count);
for(i=1;i<=count;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
getch();
}
You forgot a space between "".
for(a=rows;a>=1;a--)
space--;
printf("");
should be
for(a=rows;a>=1;a--)
space--;
printf(" ");
#include <stdio.h>
int main() {
int n = 50;
for (int i = 0; i <= n; ++i) {
for (int k = i; k < n; ++k)
printf(" ");
for (int j = 0; j < i; ++j)
printf("*");
for (int j = 1; j < i; ++j)
printf("*");
printf("\n");
}
for (int l = 1; l < n/2; ++l) {
for (int i = 1; i < n; ++i)
printf(" ");
printf("[|]\n");
}
return 0;
}
A simple tree can be made up with for loop, Christmas may need more symbol...
//Linux C program to print a tree
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
int pcenter(char *s) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
int ct = w.ws_col;
int sl = strlen(s) / 2;
printf("%*s%*s\n", ct / 2 + sl, s, ct / 2 - sl, "");
return 0;
}
int ptree(char s, char t, int l, int r) {
int i;
for (i = 1; i <= l; i++) {
int j = 2 * i - 1;
char *p = malloc(j);
memset(p, s, j);
pcenter(p);
free(p);
}
for (i = 1; i <= r; i++) {
int j = 1;
char *p = malloc(j);
memset(p, t, j);
pcenter(p);
free(p);
}
return 0;
}
int main() {
// system("clear");
ptree('*', '|', 10, 5);
return 0;
}
#include<stdio.h>
main()
{
int n,i, j, space=1;
printf("Enter the number of rows: ");
scanf("%d",&n);
space=n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=space;j++)
{
printf(" ");
}
space--;
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
for(i=1;i<=n-3;i++)
{
for(j=1;j<=10;j++)
{
printf(" ");
}
for(j=1;j<=1;j++)
{
printf("*");
}
for(j=1;j<=1;j++)
{
printf("*");
}
for(j=1;j<=1;j++)
{
printf("*");
}
printf("\n");
}
}
#include<stdio.h>
int main()
{
int i,j,k,l=1,a,b;
for(i=8;i>=0;i--)
{
for(j=0;j<=i;j++)
{
printf(" ");
}
k=0 ;
while(k<l)
{
printf("*");
k=k+1;
}
l=l+2;
printf("\n");
}
i=8;
for(b=0;b<=3;b++)
{
for(a=0;a<=i-1;a++)
{
printf(" ");
}
printf("***");
printf("\n");
}
}

Resources