Why variables sum returns multiplication? - c

in the following code:
void fusioneArray(int v[], int vL, int w[], int wL[], int *fusione)
{
int i,j,temp;
int k=0;
printf("%d",wL+vL);
for(i=0;i<vL;i++)
{
fusione[k]=v[i];
k++;
}
for(j=0;j<wL;j++)
{
fusione[k]=w[j];
k++;
}
}
int main()
{
int v[5]={1,2,3,4,5};
int w[5]={5,4,3,2,1};
int i=0;
int fusione[10];
fusioneArray(v,5,w,5,fusione);
}
can u explain me why vL+wL returns * instead of +? (25 instead of 10)...

Because wL is a pointer in your code, thus you're doing pointer arithmetics instead of a standard integer arithmetics :
wL+vL = wL + vL*sizeof(int)
Since a int is 4-bytes on most platforms your wL+vL becomes 5+5*4 = 25, which is the result you get. Simply replace int wL[] with the correct int wL and you'll have the desired behavior.

Related

Function does not update value of variable

I'm trying to write a program that multiplies two different complex numbers (b*c), stores the result in a prints out the result. For the purposes of keeping it simple here I've only decided to use 10 numbers and set all the imaginary and real values for b and c to the numbers 0 through 9.
First I create arrays that will contain the real and imaginary parts of the complex numbers a, b and c. Next I declare the real and imaginary part of a to be zero since I don't know these yet. To find these, I have a function named multiply that computes the real and imaginary parts of a.
Finally, in my main method I loop through all the arrays, generate values 0-9, use multiply to populate the arrays of a (as_re and as_im) containing the real and imaginary values of a, then simply printing all of these entries out.
However I only get: The product is 0 + 0i on every iteration. This has to mean that my multiply function does not update the values of a_re and a_im. Can anyone help me understand why?
My code is below:
#include <stdio.h>
#include <stdlib.h>
void multiply(int c_re, int c_im, int a_re, int a_im, int b_re, int b_im);
int as_re[10];
int as_im[10];
int bs_re[10];
int bs_im[10];
int cs_re[10];
int cs_im[10];
int a_re = 0;
int a_im = 0;
int main(){
for (int i = 0; i < 10; i++){
bs_re[i] = i;
bs_im[i] = i;
cs_re[i] = i;
cs_re[i] = i;
multiply(cs_re[i], cs_im[i], as_re[i], as_im[i], bs_re[i], bs_im[i]);
as_re[i] = a_re;
as_im[i] = a_im;
printf("The product is %d + %di\n", as_re[i], as_im[i]);
}
}
void multiply(int c_re, int c_im, int a_re, int a_im, int b_re, int b_im){
a_re = c_re * b_re - c_im * b_im;
a_im = c_re * b_im + c_im * b_re;
}
You don't need to pass global variables as parameters. If you declare a parameter or local variable with the same name as the global variable then it will hide the global variable in that function.
Removed the global variables which were included as parameters for your function.
#include <stdio.h>
#include <stdlib.h>
void multiply(int c_re, int c_im, int b_re, int b_im);
int as_re[10];
int as_im[10];
int bs_re[10];
int bs_im[10];
int cs_re[10];
int cs_im[10];
int a_re = 0;
int a_im = 0;
int main(){
for (int i = 0; i < 10; i++){
bs_re[i] = i;
bs_im[i] = i;
cs_re[i] = i;
cs_re[i] = i;
multiply(cs_re[i], cs_im[i], bs_re[i], bs_im[i]);
as_re[i] = a_re;
as_im[i] = a_im;
printf("The product is %d + %di\n", as_re[i], as_im[i]);
}
}
void multiply(int c_re, int c_im, int b_re, int b_im){
a_re = c_re * b_re - c_im * b_im;
a_im = c_re * b_im + c_im * b_re;
}

Sorting an array in ascending order using pointers in C

I am writing a program that creates an array of random numbers from 1 to 100 and sorts them in ascending order. Below is working code that does this, but I need to modify it so that the "swap" function makes use of pointers. The call for the swap function should look like this: swap(???,???) where the two inputs are pointers. What is the best way to accomplish this?
#include<stdio.h>
#include<math.h>
int main()
{
void fillArray(int sizeArray, int array[sizeArray]);
void printArray(int sizeArray, int array[sizeArray]);
void sortArray(int sizeArray, int array[sizeArray]);
int sizeArray;
printf("\nSize of the array? ");
scanf("%d", &sizeArray);
int array[sizeArray];
fillArray(sizeArray,array);
sortArray(sizeArray, array);
printArray(sizeArray, array);
}
void fillArray(int sizeArray, int array[sizeArray])
{
int increment;
for(increment=0; increment<sizeArray; increment++)
{
array[increment]=rand()%101;
}
}
void sortArray(int sizeArray, int array[sizeArray])
{
void swap(int increment2, int increment, int array[]);
int increment, increment2, temp;
for (increment=0; increment < sizeArray ; increment++)
{
for (increment2=increment+1; increment2 < sizeArray; increment2++)
{
swap(increment2, increment, array);
}
}
}
void swap(int increment2, int increment, int array[])
{
int temp;
if (array[increment2] < array[increment])
{
temp=array[increment];
array[increment]=array[increment2];
array[increment2]=temp;
}
}
void printArray(int sizeArray, int array[sizeArray])
{
int increment=0;
printf("\nHere's the sorted array:\n");
while(increment<21)
{
printf("\n array[%d] is %d", increment, array[increment]);
increment++;
}
}
The output should look like this:
output
Define your swap function as below:
void swap(int *increment2, int* increment)
Modify your for loop where you call the swap function:
for (increment=0; increment < sizeArray ; increment++)
{
for (increment2=increment+1; increment2 < sizeArray; increment2++)
{
swap(array[increment2], array[increment]);
}
}
Then, modify your swap function:
void swap(int *increment2, int* increment)
{
int temp;
if (increment2 < increment)
{
temp= *increment2;
*increment2=*increment;
*increment2=temp;
}
}
You need to fix your function call for the parameters to be with pointers.
void swap(int *increment2, int* increment)
Then in your swap function you need
You will need to deference the integer*.
Example
int n1;
int* x = 100
n1 = *x;
You may need to
deference in the future example
Your function accepts pointers
void swap(int *increment2, int* increment)
If you have integers or another data type to reference them, refer to their address, you can perform & for referencing.
int i = 5;
int* x;
x = &i;
x is now an integer pointer to the address of i.
Your calling code needs to pass the address of the integers to compare and swap. Either of the following forms is acceptable, and they are equivalent.
swap(array+increment2, array+increment);
swap(&array[increment2], &array[increment]);
The first form takes the address of the first element (array) and adds the index (increment2) to get the address of the correct element.
The second version is more straightforward, perhaps. It uses the & address-of operator to take the address of array[increment2], which is the desired integer.
Your swap function need to be defined as follows:
void swap(int** p2, int** p1)
{
int temp;
if (*p2 < *p1)
{
temp=*p1;
*p1=*p2;
*p2=temp;
}
}
Note how the pointers are dereferenced with the * operator to get the integer values for comparison (and storage in temp).

Allocating a 2d Array

My aim is to allocate a 2d array with only using 1 line for efficiency. Since my prof is expecting it to be efficient.
the code gives me an error saying that it can't convert from void* to int.
#include<stdio.h>
#include<stdlib.h>
#define NUMOFCOL 4
int **addtwoarr(int (*A)[NUMOFCOL], int (*B)[NUMOFCOL]);
int main(void){
int firstarr[4][4]={{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}},
secondarr[4][4]={{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}}, **receiver;
receiver = addtwoarr(firstarr, secondarr);
printf("%d", receiver[3][3]);
}
int **addtwoarr(int (*A)[NUMOFCOL], int (*B)[NUMOFCOL]){
int col, row, **arr;
(*arr)[NUMOFCOL] = malloc(NUMOFCOL * sizeof(*arr)); /*this line in particular gives the error */
for(row=0; row<NUMOFCOL; row++){
for(col=0;col<NUMOFCOL; arr[row][col]=A[row][col]+B[row][col], col++){}
}
return arr;
}
The allocation happens in the addtwoarr function which is where the error occurs.
I seriously don't recommend this, as there are a ton of assumptions in your code about top-end sizing. But if you really want to do it, the rather cryptic syntax for returning pointers to fixed length arrays in C looks something like this:
#include <stdio.h>
#include <stdlib.h>
#define NUMOFCOL 4
int (*addtwoarr(int A[][NUMOFCOL], int B[][NUMOFCOL]))[NUMOFCOL];
int main(void)
{
int firstarr[][NUMOFCOL] = {
{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}},
secondarr[][NUMOFCOL] = {
{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}};
int (*receiver)[NUMOFCOL] = addtwoarr(firstarr, secondarr);
printf("%d\n", receiver[3][3]);
free(receiver);
}
int (*addtwoarr(int A[][NUMOFCOL], int B[][NUMOFCOL]))[NUMOFCOL]
{
int col, row;
int (*arr)[NUMOFCOL] = malloc(NUMOFCOL * sizeof(*arr));
for(row=0; row<NUMOFCOL; row++){
for(col=0;col<NUMOFCOL; arr[row][col]=A[row][col]+B[row][col], col++);
}
return arr;
}
Output
2
Best of luck.

Passing a two dimensional dynamic array to a function

I don't know the array size when compiling so I declared a variable count and created an array arr[count][count], I will incrase this count variable while program is running and I will reallocate it's memory before doing that. But I couldn't create a function that takes this arr[count][count]. How can i do that ? When i did this like:
void add_friend(int friends[][*count], int p1, int p2)
{
}
compiler gives an error: count undeclared here.
You can't do what you are asking directly at least not in C. But as #millimoose suggests, if you know the dimensions (which can be passed as ints), you can do a little arithmetic to figure out where friends[p1][p2] is. If there are C elements per row, then you know that friends[r][0] is C*r ints from the start of friends, and can treat that row as a simple int array.
Well, here is a way to do what you want with a 1 dimensional array in c/c++:
void ff(int *arr_2d, int s1, int s2)
{
for(int i=0;i<s1;i++ )
{
for(int j=0;j<s2;j++ )
printf("f[%d][%d] = %d\t",i,j, *(arr_2d+i*s2+j));
printf("\n");
}
}
And you can use this function as:
int *f = new int[100];
int s1,s2;
s1 = s2 = 10;
for(int i=0; i<s1; i++)
for(int j=0; j<s2; j++)
*(f+i*s2+j) = (i*s2+j);
ff(f,10,10);
In C 1999 and in C 2011 implementations that support variable-length arrays, you can declare a function this way:
void add_friend(int count, int friends[][count], int p1, int p2) …
The count should be accessible by friends, to do that you can either declare it as a global variable or as an argument of the function.
void add_friend(int count, int friends[][count], int p1, int p2)
{
// ...
}
int main(void)
{
int a[25][10];
count = 10;
add_friend(10, a);
// ...
return 0;
}
you can do it like:
1.void add_friend(int count, int friends[count][count], int p1, int p2)
2.void add_friend(int count, int friends[][count], int p1, int p2)
3.void add_friend(int count, int (*friends)[count], int p1, int p2)

factorial giving wrong answer

#include<stdio.h>
int max = 100;
int main()
{
int a,j;
int * arr = (int*)malloc(sizeof(int)*max);
arr[max-1] = 1;
scanf("%d",&a);
factor( arr, a);
display(arr);
}
int factor( int arr[],int a)
{
if (!a) return;
int i,carry;
for(i=max-1;i>=0;i--)
{
arr[i] = (arr[i]*a) + carry;
carry = arr[i]/10;
arr[i] = arr[i]%10;
}
factor( arr, a-1);
}
int display(int arr[])
{
int i;
for ( i=0; i<max; i++)
{
printf("%d",arr[i]);
}
}
HI this is my program to find the factorial of numbers but its giving wrong answer i dont know why ...???
like when i give input as 13
then according to myprogram 13 is to be treated in array as 1 and 3 but its giving random numbers -1216731443 -121673144 . i think malloc is having problem , but i can't identify it .
thank you
I think the reason why you are getting "random" numbers is because you haven't initialized the carry variable. In the for loop, you are adding the un-initialized value of carry to the array which will cause undefined results.

Resources