A
A B
A B C
Please let me know how to print the above sequence.
Condition for spaces should be as below
A(no space)
A(space)B(no space)
A(space)B(space)C(no space)
#include <stdio.h>
int main() {
int i = 65; //'A' = 65 ASCII value
for (int x = 1; x < 10; x++) {
for (int y = 0; y < x; y++) {
printf("%c", (char)i);
i++;
if (y < (x-1)) printf(" ");
}
printf("\n");
i = 65;
}
return 0;
}
Instead of 10 x < 10, insert how many lines with letters you want to be printed
Related
How do I make my code more efficient (in time) pertaining to a competitive coding question (source: codechef starters 73 div 4):
(Problem) Chef has an array A of length N. Chef wants to append a non-negative integer X to the array A such that the bitwise OR of the entire array becomes = Y .
Determine the minimum possible value of X. If no possible value of X exists, output -1.
Input Format
The first line contains a single integer T — the number of test cases. Then the test cases follow.
The first line of each test case contains two integers N and Y — the size of the array A and final bitwise OR of the array A.
The second line of each test case contains N space-separated integers A_1, A_2, ..., A_N denoting the array A.
Please don't judge me for my choice of language .
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int* binary_number(int n) // returns pointer to a array of length 20(based on given constrains) representing binary
{
int* ptc;
ptc = (int*) malloc(20*sizeof(int));
for(int i = 0; i < 20; i++)
{
if((n / (int) pow(2,19-i)) > 0){*(ptc + i) = 1;}
else {*(ptc + i) = 0;}
n = n % (int) pow(2,19-i) ;
}
return ptc;
}
int or_value(int* ptc, int n) // Takes in pointers containing 1 or zero and gives the logical OR
{
for(int k = 0; k < n; n++)
{
if(*ptc == *(ptc + 20*k)){continue;} // pointers are 20 units apart
else{return 1;break;}
}
return *ptc;
}
int main(void) {
int t; scanf("%d", &t);
for (int i = 0; i < t; i++)
{
int n, y;
scanf("%d %d", &n, &y);
int a[n];
for(int j = 0; j < n ; j++)
{
scanf("%d", &a[j]);
}
int b[20*n];
for (int j = 0; j < n; j++)
{
for (int k = 0; k < 20; k++)
{
b[20*j + k] = *(binary_number(a[n])+k);
}
}
int c = 0;
int p = 0;
for (int j = 0; j < 20; j++)
{
if ((*(binary_number(y) + j) == 1) && (or_value((&b[0] + j),n) == 0)){c = c + pow(2,19 - j);}
else if ((*(binary_number(y) + j) == 0) && (or_value((&b[0] + j),n) == 1)){p = 1; break;}
}
if (p==1){printf("-1");}
else {printf("%d\n", c);}
}
return 0;
}
I'm trying to generate a random 2D array, which is filled randomly with special symbols.
Now the problem is that when I compile my code, it prints my array, but the symbols are filled from top to bottom. That is wrong, it has to start filling them from bottom. And the other problem is that each special symbol "!##$" should be filled in equally, which is determined by the number of rows of an array -> "field". So for example when an array is generated with 4 rows, then each symbol should be randomly scattered exactly 4 times.
I've been trying to fix those two issues, but I just don't know how. So I'd be very thankfull for someone, who could point me towards fix and help me understand where my mistakes are.
My code :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int main() {
srand(time(NULL));
int y, x, a=0, b=0;
y = (rand()%(8-3+1)+3); // y = number of rows
x = (rand()%(8-4+1)+4); // x = number of columns
char field[y][x];
const char* symbol = "!##$";
int r; // determines how many symbols are used based on
if(x == 7) // number of columns - 2 = number of symbols used
r = x-3;
else if(x > 6)
r = x/2;
else
r = x-2;
int randnum1 = (rand()%x), randnum2 = (rand()%x);
if(randnum1 == randnum2) { // takes 2 random indexes from number of columns
randnum2 += 1; // and fills them with blanks
}
if(randnum2 >= x) {
randnum2 -= 2;
}
int num1 = 0, num2 = 0, num3 = 0, num4 =0; // checks if same number of all
// symbols is used
for(a = 0; a < y; a++) {
printf("\n %d |", a+1);
for(b = 0; b < x; b++) {
if(b == randnum1 || b == randnum2) {
field[a][b] = ' ';
printf(" %c |", field[a][b]);
}
else {
field[a][b] = symbol[rand()%r];
int sum1 = num1+num2+num3+num4; // sum checks if the total max
int sum2 = y * r; // number of symbols has been used
if(field[a][b] == '!')
num1 += 1;
if(field[a][b] == '#')
num2 += 1;
if(field[a][b] == '#')
num3 += 1;
if(field[a][b] == '$')
num4 += 1;
if(sum1 < sum2) {
if((num1 < y) || (num2 < y) || (num3 < y) || (num4 < y)) {
printf(" %c |", field[a][b]);
}
else {
field[a][b] = ' ';
printf(" %c |", field[a][b]);
}
}
else {
field[a][b] = ' ';
printf(" %c |", field[a][b]);
}
}
}
}
putchar('\n');
for(a = 0; a < 1; a++) {
printf(" ");
for(b = 0; b < x; b++) {
printf("--- ");
}
}
putchar('\n');
for(b = 0; b < 1; b++) {
printf(" ");
for(b = 0; b < x; b++) {
printf(" %d ", b+1);
}
}
putchar('\n');
return 0;
}
#include "stdio.h"
int main() {
int max = 1000;
for (int i = 0; i < max; ++i) {
printf("%d", i);
}
return 0;
}
If max is 1000 then this will print in the format shown below
0123 up to 1000
But I would like to print 4 values per line as shown below:
0123
4567
...
I would like to see the numbers not the just the digits. for a single digit numbers, it should be like this: 0123 for two digit numbers, it should be like this: 11121314 for a three digit numbers, it should be like this: 111112113114 up to 996997998999 up to 1000.
For your loop to print upto and including 1000 for max.size = 1000, you must use the <= operator.
Here is a modified version that will format the output with a maximum of 4 characters per line:
#include <stdio.h>
#include <limits.h>
int main(void) {
struct { int size; } max = { 1000 };
if (max.size >= 0) {
for (int col = 0, i = 0;; i++) {
char buf[2 + sizeof(int) * CHAR_BIT / 3];
int n = snprintf(buf, sizeof buf, "%d", i);
for (int j = 0; j < n; j++) {
putchar(buf[j]);
if (++col == 4) {
putchar('\n');
col = 0;
}
}
if (i == max.size) {
if (col > 0) {
putchar('\n');
}
break;
}
}
}
return 0;
}
It will print:
0123
4567
8910
1112
1718
...
6997
9989
9910
00
EDIT
From your updated question, it is actually much simpler: print a linefeed character after every 4th number, using the modulo operator %.
#include <stdio.h>
int main(void) {
int max = 1000;
for (int i = 0; i < max; ++i) {
printf("%d", i);
if (i % 4 == 3)
putchar('\n');
}
return 0;
}
Just check whether i+1 is divisible by 4 or not. Whenever it is divisible by 4, print a newline.
for (int i = 0; i < max.size; ++i) {
printf("%d", i);
if((i+1)%4 == 0)
printf("\n");
}
You can also do this without using a buffer:
#include <stdio.h>
void print_digit(int number);
int main(void) {
putchar('0');
int i;
for(i = 1; i <= 1000; i++) {
print_digit(i);
}
}
void print_digit(int number) {
static int digit_count = 1; // a zero is already printed
int i;
for(i = 1; i <= number; i *= 10);
for(i /= 10; i; i /= 10) {
putchar('0' + number % (i * 10) / i);
digit_count++;
if(digit_count == 4) {
digit_count = 0;
putchar('\n');
}
}
}
However, I have to admit that this code has nothing to do with elegance, because I don't know how to make print_digit consistent with zero.
int main(void)
{
int i,j=0,k; //initialization
char equation[100]; //input is a string (I think?)
int data[3]; //want only 3 numbers to be harvested
printf("Enter an equation: ");
fgets(equation, 100, stdin); //not so sure about fgets()
for (i = 0; i < equation[100]+1; i++) { //main loop which combs through
//"equation" array and attempts
//to find int values and store
while (j <= 2) { //them in "data" array
if (isdigit(equation[i])) {
data[j] = equation[i]
j++;
}
}
if (j == 2) break;
}
for (k = 0; k <= 2; k++) { //this is just to print the results
printf("%d\n", data[k]);
}
return 0;
}
Hello! This is my program for my introductory class in C, I am trying to comb through an array and pluck out the numbers and assign them to another array, which I can then access and manipulate.
However, whenever I run this I get 0 0 0 as my three elements in my "data" array.
I am not sure whether I made an error with my logic or with the array syntax, as I am new to arrays.
Thanks in advance!!! :)
There are a few problems in your code:
for (i = 0; i < equation[100]+1; i++) { should be something like
size_t equ_len = strlen(equation);
for (i = 0; i < equ_len; i++) {
Whatever the input is, the value of equation[100] is uncertain, because char equation[100];, equation only has 100 element, and the last of them is equation[99].
equation[i] = data[j]; should be
data[j] = equation[i];
I suppose you want to store digit in equation to data.
break; should be deleted.
this break; statement will jump out of the while loop, the result is you will store the last digit in equation to data[0] (suppose you have switched data and equation, as pointed out in #2).
If you want the first three digits in equation, you should do something like
equ_len = strlen(equation);
j = 0;
for (i = 0; i < equ_len; i++) {
if (j <= 2 && isdigit(equation[i])) {
data[j] = equation[i];
j++;
}
if (j > 2) break;
}
printf("%d\n", data[k]); should be printf("%c\n", data[k]);
%d will give the ASCII code of data[k], for example, if the value of data[k] is character '1', %d will print 50 (the ASCII code of '1') instead of 1.
Here is my final code, based on the OP code:
#include <ctype.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
int i,j,k;
char equation[100];
int data[3];
int equ_len;
printf("Enter an equation: ");
fgets(equation, 100, stdin);
equ_len = strlen(equation);
j = 0;
for (i = 0; i < equ_len; i++) {
if (j <= 2 && isdigit(equation[i])) {
data[j] = equation[i];
j++;
}
if (j > 2) break;
}
for (k = 0; k <= 2; k++) {
printf("%c\n", data[k]);
}
return 0;
}
Tested with:
$ ./a.out
Enter an equation: 1 + 2 + 3
1
2
3
I'm trying to write a program in C that gives a 13 row pyramid with the following output (notice the pattern of the letters i.e BCB):
A
BCB
DEFED
GHIJIHG
KLMNONMLK
PQRSTUTSRQP
VWXYZABAZYXWV
CDEFGHIJIHGFEDC
KLMNOPQRSRQPONMLK
TUVWXYZABCBAZYXWVUT
DEFGHIJKLMNMLKJIHGFED
OPQRSTUVWXYZYXWVUTSRQPO
ABCDEFGHIJKLMLKJIHGFEDCBA
Here is my attempt at the solution:
#include <stdio.h>
#include <stdlib.h>
int main(void){
char c = 'A';
int height = 13;
int max = 1;
for (int i = 1; i <= height; i++){
//int j = 1;
for (int k = 0; k < height - i; k++)
printf(" "); // print space on left
for (int j = 1; j <= max; j++){
if (j <= max / 2){ // print left side of pyramid
printf ("%c", c);
c = (c - 'A' + 1) % 26 + 'A';
}
else{ // print right side of pyramid
printf ("%c", c);
c = (c -'A' + 25) % 26 + 'A';
}
}
printf("\n");
max += 2;
}
}
However it gives the following incorrect output:
A
ZAZ
YZAZY
XYZAZYX
WXYZAZYXW
VWXYZAZYXWV
UVWXYZAZYXWVU
TUVWXYZAZYXWVUT
STUVWXYZAZYXWVUTS
RSTUVWXYZAZYXWVUTSR
QRSTUVWXYZAZYXWVUTSRQ
PQRSTUVWXYZAZYXWVUTSRQP
OPQRSTUVWXYZAZYXWVUTSRQPO
if I remove the the if/else statement which splits the pyramid in to two sides and simply have only c = (c - 'A' + 1) % 26 + 'A';, I get the following output:
A
BCD
EFGHI
JKLMNOP
QRSTUVWXY
ZABCDEFGHIJ
KLMNOPQRSTUVW
XYZABCDEFGHIJKL
MNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLM
Any ideas?
The problem is that you're forgetting to increment the actual overall character. For each line, you need to add characters until you get to the value that you should start at for the next line. Thankfully, this is pretty easy to do:
...
max += 2;
c = (c - 'A' + max / 2 + 1) % 26 + 'A'; // Add this line
}
Here's a short version:
#include <stdio.h>
#define HEIGHT 6
int main(void) {
char* f = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* b = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
int c = 0;
for(int i=0; i<HEIGHT; ++i)
{
printf("%*.*s%.*s\n", 10, i+1, f+i+c, i, b+strlen(b)-(c+2*i));
c+=i;
}
return 0;
}
IDEOne Link
Output:
Success #stdin #stdout 0s 9424KB
A
BCB
DEFED
GHIJIHG
KLMNONMLK
PQRSTUTSRQP