I'm trying to display the integer like
1111
222
33
4
using loop but i dont know what is wrong with my code, i'm new to c programming and thanks for your help :D
Here is my code :
#include <stdio.h>
int main()
{
int a, b;
for (int a=4; a<=1; a--)
{
for (int b=1; b<=a; b++) {
printf("%d", b);
}
}
printf("\n");
}
What is wrong:
#include <stdio.h>
int main()
{
int a, b;
for (int a=4; a<=1; a--) /* a<=1 is false when a=4 */
{
for (int b=1; b<=a; b++) {
printf("%d", b); /* what is printed depends on b while it shouldn't */
}
}
printf("\n"); /* this is in wrong place */
}
Fixed code:
#include <stdio.h>
int main()
{
int a, b;
for (int a=4; a>=1; a--)
{
for (int b=1; b<=a; b++) {
printf("%d", 1 + (4 - a));
}
printf("\n");
}
}
First, the condition in the first loop "a<=1" will never be true since you are initializing a=4.
for (int a=4; a<=1; a--)
it will get out of the loop in the first iteration as a=4 and 4 is not less then 1
one of the way to print this is....
#include <stdio.h>
int main()
{
int a, b;
for (a=1; a<=4; a++)
{
for (b=4; b<=1; b--) {
printf("%d", a);
}
}
printf("\n");
printf("Hello, World!\n");
return 0;
}
Your code has four issues.
1.
You declare a and b twice. One time before and one time inside of the loops. The ones you use are actually the variables you declare/define inside of the loop's initialization parts. The variables a and b declared outside are completely redundant and unused in your actual code.
2.
The condition of the outer for loop a <= 1 will never be true and you will not at least go one time through the loops because a is initialized with 4 and since 4 is not less or equal than 1 you will have no iteration at all.
The condition needs to be a >= 1, not a <= 1.
3.
printf("\n"); needs to be placed inside of the outer loop and not outside of it.
4.
The logic of your inner for loop is flaw. We need a counter inside of the inner loop and test it against a condition instead to use and increment b. Doing so would gives you only numbers in ascending order in each line.
Increment b after the inner loop is complete.
Corrected code (Online test):
#include <stdio.h>
int main (void)
{
int a = 4, b = 1;
for ( ; a >= 1; a-- ) {
for ( int i = 0; i < a; i++ ) {
printf("%d", b);
}
b++;
printf("\n");
}
}
Execution:
./a.out
1111
222
33
4
Related
In this example I want to print out the number 4. This is a simplified version of my problem, but my issue is the same. After assigning b a value (in this case 4), I want to print out the 4th element of the array, not directly, but by using a separate integer (c). However a 0 gets printed out as a result. I have no idea why. Would be glad if you could help. Thanks a lot in advance!
#include <stdio.h>
#include <stdlib.h>
int numbers[10], a, b, c;
int main() {
for (a = 0; a < 11; a++) {
numbers[a] = a;
}
b = 7 - 3;
numbers[b] = c;
printf("%d", c);
return 0;
}
Instead of setting the 4th element of the array, you should read it and store it into c:
c = numbers[b];
Also note that the initialization loop runs one step too far, assigning non-existent element numbers[10].
Here is a modified version:
#include <stdio.h>
int main() {
int numbers[10], a, b, c;
for (a = 0; a < 10; a++) {
numbers[a] = a;
}
b = 7 - 3;
c = numbers[b];
printf("%d\n", c);
return 0;
}
The program needs to show an number which has a greater first digit, which works, only if the numbers of digits are the same on both variables. Try with these numbers 4506 29985, it should show 4506, but the program shows 29985, the program was written in C language.
#include <stdlib.h>
#include <stdio.h>
int disassembly1 (int a)
{
while (a>=10)
{
a /= 10;
}
return a;
}
int disassembly2 (int b)
{
while (b>=10)
{
b /= 10;
}
return b;
}
int main ()
{
int A1, B1, a, b;
printf("Int 2 numbers:\n");
scanf_s("%d %d", &A1, &B1);
a = A1;
b = B1;
disassembly1(A1);
disassembly2(B1);
if (a > b || ((a == b) && (A1 > B1)))
printf("%d\n", A1);
else
printf("%d\n", B1);
system("pause");
}
First of all, disassembly1 and disassembly2 are the same function and should not written twice.
Second and more important, the function disassembly returns the first digit, but you need to store it in a variable.
A working code (with a few changes):
#include <stdlib.h>
#include <stdio.h>
int disassembly(int a)
{
while (a>=10)
{
a /= 10;
}
return a;
}
int main()
{
int a, b, a_digit, b_digit;
printf("Int 2 numbers:\n");
scanf_s("%d %d", &a, &b);
a_digit = disassembly(a);
b_digit = disassembly(b);
if (a_digit > b_digit || ((a_digit == b_digit) && (a > b)))
printf("%d\n", a);
else
printf("%d\n", b);
system("pause");
return 0;
}
Also, the name disassembly does not match the function meaning. I would suggest to change it to something like get_first_digit.
Your code does not read the values returned from the functions, leaving variables a & b unchanged.
Change:
disassembly1(A1);
disassembly2(B1);
To:
a = disassembly1(A1);
b = disassembly2(B1);
Other suggestions:
define and use a single instance of function: int disassembly(int x); and call it twice.
a = disassembly(A1);
b = disassembly(B1);
Here is a much simpler version (this is matlab/octave, but can easily be ported to c)
a = 4506;
b = 29985;
isAbigger = floor(a/10^(floor(log10(a)))) > floor(b/10^(floor(log10(b))));
I am passing in an array into a function straightflush. I use a counting loop so i can get to all of the elements but for some reason, even tho the counter i increases, i get the value and suit for the first element of the array. Therefore, only my spadesCount increases as it always shows 4 for the value and spade for the suit.
struct card{
int value;
char suit;
};
int straightflush(struct card hand[], int n)
{
int clubsCount = 0;
int diamondsCount = 0;
int heartCount = 0;
int spadesCount =0;
int i;
for(i=0; i<n; i++)
{
if (hand[i].suit == 'c')
{
clubsCount++;
}
else if (hand[i].suit == 'd')
{
diamondsCount++;
}
else if (hand[i].suit == 'h')
{
heartCount++;
}
else{
spadesCount++;
}
}
return 0;
}
here is my main:
int main(){
struct card hand1[] = {{4,'s'}, {9,'s'},{12,'c'},{11,'s'},{8,'s'},
{6,'d'}, {3,'d'},{7,'s'},{10,'s'},{12,'d'}};
printf ("%d\n", straightflush(hand1, 10));
}
I just run your code and the four count variables have correct values. I think it's because you are returning 0 at the end of your straightflush function, the output is always 0.
You can use a debugger or add the following line before the return statement in straightflush() to prove that your counts are actually accurate.
printf("%d %d %d %d\n", clubsCount, diamondsCount, heartCount, spadesCount);
Your return value has nothing to do with the values you read thus the printf statement in your main() function is not printing the count of any thing, it is just printing 0 no matter what.
If you want the counts accessible outside of striaghtflush() you need to either use global variables for those counts (a generally shunned idea) or pass some values in by reference. An example of this would be:
#include <stdio.h>
#include <stdlib.h>
void editValues( int *numDiamonds, int *numClubs, int *numHearts, int *numSpades ){
*numDiamonds = 3;
*numClubs = 5;
*numHearts = 7;
*numSpades = 11;
}
int main(int argc,char**argv)
{
int numD=0, numC=1, numH=2, numS=3;
printf("%d %d %d %d\n", numD, numC, numH, numS);
editValues(&numD, &numC, &numH, &numS);
printf("%d %d %d %d\n", numD, numC, numH, numS);
return 0;
}
I have been struggling for a while with my program. I am trying to find horizontal pairs in an array that is setup in function1. My goal is to change the original array in another function. Then process that array to find a horizontal pair.
One problem that has occurred is when I run the program, the result is zero. Another problem is the gcc warning in function 1, warning: comparison between pointer and integer. The other problem is another gcc warning (marked by the **) warning: passing argument 1 of 'function1' from incompatible pointer type.
I appreciate any help, as a beginner, I have spent several hours on this problem and have tried to find solutions, but trying to use pointers and using struct and typedef have not worked. :(
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void function1 (int letter1[][16]);
void function2 (int letter2[][16]);
int main ( void )
{
int letter_array [13][16];
printf("\n \t\t Hello!");
printf("\n This program will create a random selection of 180 upper-case"
" characters. \n");
**function1(&letter_array);**
function2(letter_array);
return ( 0 ) ;
}
void function1 (int letter1 [][16])
{
int i, z;
srandom((unsigned)time(NULL));
for(i=0; i<=11; i++)
{
for(z=0; z<=14; z++)
{
letter1 [i][z] = random( )%26+65;
printf("%c ", letter1 [i][z]);
}
printf("\n");
}
return ;
}
void function2 (int letter2 [][16])
{
int a,b;
int m=0;
for( a = 0; a <= 11; a++)
{
for( b = 0 ; b <= 14; b++)
{
if (letter2 == (letter2 + 1))
m++;
}
}
printf("\nThe number of horizontal pairs of characters"
" are: %d", m);
return ;
Just remove the ampersand & from the argument.
Change
function1(&letter_array);
to
function1(letter_array);
EDIT:
Also change
if (letter2 == (letter2 + 1))
to
if (letter2[a][b] == (letter2[a][b+1]))
The warning is occurring because you are passing a pointer to an array[][16] into function1 instead of the array itself. This can be resolved by removing the &:
function1(letter_array);
The program is returning 0 because of the return statement at the end of your main function.
im a 1st grader when it comes to c and need help with storing 5 random values in an array and outputting them. Heres where am at.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct score_card {int A_ones; int B_twos; int C_threes; int D_fours; int E_fives; int F_sixes; int G_chance;};
int dice_rolls[5];
int randomize(void);
int value;
int main(void) {
struct score_card test;
randomize;
int i;
for(i = 0; i <= 4; i++){
printf("%d\n", dice_rolls[i]);
}
printf("\n");
return 0;
}
int randomize(void){
int i;
srand(time(0));
for(i = 0; i <= 4; i++){
value = rand() % 6 + 1;
dice_rolls[i] = value;
}
}
The output is :
6294304
6294308
6294312
6294316
6294320
the goal was to use modular division to get values from 1 -->6 and store them in the dicerolls array.
I see two immediate problems.
First. you're not terminating your random numbers with a newline. That's why they're all strung together in a big sequence. Change your output line to:
printf("%d\n", &dice_rolls[i]);
Secondly, you're not actually calling randomize. The correct way to call it is with:
randomize();
The statement randomize; is simply an expression giving you the address of the function. It's as useless in this case as the expression 42; which also does nothing. However it's valid C so the compiler doesn't necessarily complain.