Weird results using simple math operations - c

i have the following struct:
typedef struct number
{
int x;
int y;
int z;
unsigned long int final;
}number;
my code is the following:
number* numbers;
numbers= (number*)malloc(sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, &numbers->y, &numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d",numbers->final);
but the output is wrong. for example here is a run:
12 12 12
input: 12,12,12
final: -28640
i cant figure out the problem. the highest number that number->final can get is 90,000 (i make sure of it as i gives the input)... i seems like there is overlap? please help.

Your problem is the pointer. I am assuming you initialised the struct as follows.
numbers *numbers;
However if you use it in the main where you declare it don't use a pointer. There are also a few errors in your printf call, you are printing the memory address of y and z instead of the value like you did for the x value.
Use something like this.
#include <stdio.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number numbers;
scanf("%d %d %d", &numbers.x, &numbers.y, &numbers.z);
printf("input: %d,%d,%d\n",numbers.x, numbers.y, numbers.z);
numbers.final=(numbers.x)*4000 + (numbers.y)*50 + (numbers.z);
printf("final: %d\n",numbers.final);
return 0;
}
Right and if you used malloc it looks like this.
#include <stdio.h>
#include <stdlib.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number *numbers = malloc(1 * sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, numbers->y, numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d\n",numbers->final);
free(numbers);
return 0;
}
Running example here

The reason for your wrong answer is because you have kept the datatype as int which has max value of 32767 change it to unsigned long int as your ans calculates to 2400612

Related

Error "too few arguments to function call, at least argument 'format' must be specified" C programming

#include <cs50.h>
//declare functions
int add_two_ints(int a, int b);
int main(void)
{
//ask the user for input
printf("give an integer: ");
int x = get_int();
printf("give me another integer: ");
int y = get_int();
//call function
int z = add_two_ints(x, y);
printf("the result of %i plus %i is %i!\n", x, y, z);
}
//function
int add_two_ints(int a, int b)
{
int sum = a + b;
return sum;
}
when i run the program i get the error too few arguments to function call, at least argument format must be specified
this is a simple function with only two arguments being pass since im new to c programming im trying to figure out where i made the mistake.
whats is the correct way to write function ?
The get_int function included as part of CS50 expects a string for a prompt, which you're not passing. So instead of this:
printf("give an integer: ");
int x = get_int();
You want this:
int x = get_int("give an integer: ");
And similarly for reading y.
I just realize what I was doing wrong the get_int was expecting a string so I just deleted the printf statement and put in on to the get_int so now when it runs it works
#include <stdio.h>
#include <cs50.h>
//declare functions
int add_two_ints(int a, int b);
int main(void)
{
//ask the user for input
int x = get_int("give an integer: ");
int y = get_int("give an integer: ");
int z = add_two_ints(x, y);
printf("the result of %i plus %i is %i!\n", x, y, z);
}
int add_two_ints(int a, int b)
{
int sum = a + b;
return sum;
}

C - Beginner question, calling my print function wrong

Code isn't done, so variables like KWHPrice can be ignored.
When I'm trying to run my code only the first print is displayed correctly, if I enter let's say 3 4 6 2 5, I get 4194432 (address), is suspect it's because I'm referring to int smallest wrong, as it's both a variable in main and in the function, hence two different variables. I would like some guidance
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest);
int main(void){
int num, smallest;
printf("\nEnter no of elements :");
scanf("%d", &num);
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest);
printf("\nSmallest Element : %d", smallest);
}
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest){
for (i = 0; i < num; i++)
scanf("%d", &KWHPrice[i]);
smallest = KWHPrice[0];
for (i = 0; i < num; i++) {
if (KWHPrice[i] < smallest) {
smallest = KWHPrice[i];
}
}
}
You are redeclaring GET_LOWEST_PRICE in main. To use it you will need to call it with your specified parameters.
ex: GET_LOWEST_PRICE(10.8, 1, 1.8, 0.2)

How to implement a^b without `pow`?

I need to write a function to compute a^b but I am not allowed to use pow. Any ideas? I am lost.
It looks like problem is in main now...
Somewhere it gets that vys is what i characterise it. So if i set that vys=1 in main i get 1 in output..
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
void multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *=b;
i++;
}
return vys;
}
main(void)
{
int b=0, n=0, vys=1;
printf("Give numbers b and n but they must be in interval <0,10>!\n");
scanf("%d %d", &b, &n);
if ((b < 0 || b>10) || (n<0 || n>10))
{
printf("Numbers are not in interval <0,10>!\n");
}
else
{
printf("Number is in interval so i continue...\n");
sleep(2);
vys= multiplied(&b, &n);
printf("%d", vys);
}
Let's be explicit.
First, this
void multiplied(int *b, int *n)
returns an int, so say so.
int multiplied(int *b, int *n)
Next, you initialised variables in main: do the same here.
int i, vys;
Like this:
int i=1, vys=1;
Now let's look at the loop:
while (i<=n)
{
vys=*b**b;
i++;
}
As it stands, you are setting vys to something over and over again in the loop.
You want to multiply up, e.g. 2, then 2*2, then 2*2*2, .... if you want a power of two:
while (i<=n)
{
vys *= *b;
i++;
}
Now, you don't need to pass pointers.
int multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *= b;
i++;
}
return vys;
}
Edit:
Watch out for when you call the function:
main(void)
{
int b=0, n=0, vys;
//input and checking code as you have it
multiplied(&b, &n); //<---- return ignored
printf("%d", vys); //<-- print uninitialsed local variable
}
Change you last two lines:
vys = multiplied(&b, &n); //<---- return captured
printf("%d", vys); //<-- print returned variable
Edit 2:
With the change to use int in the function and not pointers, pass the ints not their addresses:
vys = multiplied(b, n); //<---- pass the ints not their addresses
printf("%d", vys); //<-- print returned variable, which should vary now
Here you have a simple code:
#include <stdio.h>
long long intpow(int a, int b)
{
long long tempres = 1;
while(b--)
tempres *= a;
return tempres;
}
int main(void) {
printf("%lld\n", intpow(5,10));
return 0;
}
you need much larger int to accommodate the result.
You can play with it yourself: https://ideone.com/4JT6NQ

Structures and Strings in C

I started learning C and I wrote a short program implementing structures. For now, it only has a structure and a short function that is supposed to fill one of the structures (several are stored in an array). Program gets all the values (currently only for one structure to help me see what's going on) and the problem starts in main, in
printf("%s", tablica[0].nazwa);
Because program stops responding (no errors or warnings before).
If I put:
printf("%d", tablica[0].x);
It will print the value I put as x, so I know there is some problem with string in printf (but I can't figure out why). It's probably easy, but I'm just a beginner.
#include <stdio.h>
#include <string.h>
struct struktura
{
char *nazwa;
double x, y, z;
};
int wczytaj(struct struktura tab[])
{
int i;
for ( i = 0; i<1; i++)
{
printf("Podaj nazwe: ");
scanf("%s", &tab[i].nazwa);
printf("Podaj x: ");
scanf("%i", &tab[i].x);
printf("Podaj y: ");
scanf("%i", &tab[i].y);
printf("Podaj z: ");
scanf("%i", &tab[i].z);
};
return 0;
}
int main(struct struktura* a)
{ int i;
struct struktura tablica[6];
int wyniki[6][6];
wczytaj(tablica);
printf("%s", tablica[0].nazwa);
}
Sorry for some names being in Polish, I can correct that, but I hope it doesn't blur the program).
You are using the wrong format specifiers for double types in
scanf("%i", &tab[i].x);
printf("%d", tablica[0].x);
and others. They should be
scanf("%lf", &tab[i].x);
printf("%f", tablica[0].x);
Also this string input
scanf("%s", &tab[i].nazwa);
should lose the & ampersand like this
scanf("%s", tab[i].nazwa);
but even so nazwa has no memory allocated to it. As suggested in comments you could get going with a fixed array like
struct struktura
{
char nazwa[30];
double x, y, z;
};
You have a very unsual signature for main, which is usually
int main(void)
or
int main(int argc, char *argv[])

Error: Storage size of a isn't known

I have been searching for the problem with this code but I'm just not sure what to do now. I was doing some practice with structures in C.
It's intended to be a program that gets the values of 2 integers and adds them and stores the value in sum and then print sum.
#include <stdio.h>
#include <string.h>
int main()
{
typedef struct {
int num;
int num2;
int sum;
} calc;
struct calc a;
printf("Enter number: ");
scanf("%d", &a.num);
printf("\nEnter a number: ");
scanf("%d", &a.num2);
a.sum=a.num+a.num2;
printf("Output: %d", a.sum);
return 0;
}
Error: Storage size of a isn't known.
typedef struct {
int num;
int num2;
int sum;
} calc;
Here, you are giving an anomymous structure an alias of calc, so there's no such structure called struct calc. You should define a as:
calc a;
Or, you can give your structure a tag:
typedef struct calc {
int num;
int num2;
int sum;
} calc;
Now you can use either calc or struct calc.
You have typedef'ed your struct, so when you declare it, just use calc instead of struct calc.
Change struct calc a; to calc a;

Resources