Arrays and integers in c - c

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

Related

The program needs to show the variable with greater first digit

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

How to display integers with pattern using loop?

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

How to multiply 2 numbers using recursion

I'm trying to multiply (3, 6) and (9, 9) using recursion. However, the result printed is 18 and 45. I need to find out which part is wrong.
Here's my code:
#include <stdio.h>
int multiply (int, int);
int main()
{
int a, b, c;
a = 6; b = 3; c = multiply(a, b);
printf("%d\n", c);
a = 9; b = 9; c = multiply(a, b);
printf("%d\n", c);
return 0;
}
int multiply(int a, int b)
{
static int c = 0, i = 0;
if (i < a) {
c = c + b;
i++;
multiply(a, b);
}
return c;
}
The issue is that multiply's static variables persist from call to call, which throws the second calculation off. It is possible to bandage this wound, but it's better to address the underlying design problem that is compelling use of static variables in the first place. There is no need to artificially maintain state in the function using i (the number of additions to perform) and c (a product accumulator).
Given that multiplication is repeated addition of a b times, you can establish a base case of b == 0 and recursively add a, incrementing or decrementing b (depending on b's sign) until it reaches 0. The product accumulator c is replaced by the function return value and the number of multiplications i is represented by b.
Using this approach, each stack frame's state is naturally self-reliant.
#include <stdio.h>
int multiply(int a, int b) {
if (b > 0) {
return a + multiply(a, b - 1);
}
else if (b < 0) {
return -a + multiply(a, b + 1);
}
return 0;
}
int main() {
printf("%d\n", multiply(3, 6));
printf("%d\n", multiply(9, 9));
printf("%d\n", multiply(-6, 2));
printf("%d\n", multiply(6, -2));
printf("%d\n", multiply(-7, -3));
printf("%d\n", multiply(0, 7));
printf("%d\n", multiply(7, 0));
printf("%d\n", multiply(0, 0));
return 0;
}
Output:
18
81
-12
-12
21
0
0
0
As a final note, I recommend following proper code style. Minifying your code and using single-character variable names only makes debugging more difficult (someone has since de-minified the original code in an edit).
Both c and i need to be reset to zero on each [outer] call to multiply [as others have mentioned] because a function scope static variable is only initialized once.
There is no way to do this because the static variables are at multiply function scope (i.e. how does main access/reset them?). They would need to be moved to global/file scope.
Adding a helper function and moving the variables to global scope will do it:
#include <stdio.h>
int multiply(int, int);
int
main()
{
int a,
b,
c;
a = 6;
b = 3;
c = multiply(a, b);
printf("%d\n", c);
a = 9;
b = 9;
c = multiply(a, b);
printf("%d\n", c);
return 0;
}
static int c, i;
int
mul(int a, int b)
{
if (i < a) {
c = c + b;
i++;
mul(a, b);
}
return c;
}
int
multiply(int a, int b)
{
i = 0;
c = 0;
return mul(a,b);
}
Try resetting your static variables before second call to multiply or do without them
int multiply(int a, int b) {
If (a==0)
return 1;
else if (a>0)
return b+multiply(a-1, b);
else
return - 1*multiply(-1*a, b); }

Error with ToString()

I'm new to c. Please help me
Why do I get this error using eclipse
Multiple markers at this line
- request for member 'ToString' in something not a structure or union
- Method 'ToString' could not be resolved
Here is my code
#include <stdio.h>
int main()
{
int s = 5;
int n = 4;
char g = s.ToString();
char l = n.ToString();
printf(g+l);
return 0;
}
s and n are just ints; they don't have a ToString() method. Also, as #remyabel pointed out, char is not the appropriate type for storing a string value, anyway; it stores only one character.
You don't need to convert your ints to strings at all to do what you're trying to accomplish, so you actually want something like this:
#include <stdio.h>
int main()
{
int s = 5;
int n = 4;
printf("%d%d", s, n); // you can't add l to g here!
return 0;
}
// output 54
DEMO
Oh, and please use more descriptive variable names!
EDIT: To save the string, as requested in the comments, you could do this:
char myString[10];
sprintf(myString, "%d%d", s, n); // myString is now "54"
I'd suggest picking up a C tutorial and starting from the beginning. The use of ToString isn't the only thing that's wrong. You could rewrite it this way and it should work (assuming you want to print "54"):
#include <stdio.h>
int main()
{
int s = 5;
int n = 4;
char g = '0' + s;
char l = '0' + n;
printf("%c%c", g, l);
return 0;
}
But this only works as long as s and n are less than 10, and besides is overly complicated since printf is made for formatting and printing values of different types. This would work just as well:
#include <stdio.h>
int main()
{
int s = 5;
int n = 4;
printf("%d%d", s, n);
return 0;
}
If you want to use the string for something else than printing, the answer depends on what you want to do.

Beginner Here Rand in c

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.

Resources