Printing Arrays in separate Function in C - c

I'm trying to print all of the values in the four arrays by sending them to a separate function. But, the problem is I can't get the function to print the all of the integers in the array because I'm not sure what I could set the condition statement in the for loop to, that would be universal to any array of any size.
Right now the function only prints the first 11 numbers. I assume that's because the first number in that array is 11.
#include <stdio.h>
void print_array(int a[]);
void find_max(int b[]);
void find_min(int c[]);
void search(int d[]);
void SORT(int e[]);
int main(void)
{
int first[11] = {7,7,7,7,7,7,7,7,7,7,7};
int second[14] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
int third[16] = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int fourth[23] = {-3, 4, 33, 22, 9, -100, 2, 56, 57, 55, 2, 90, 2234, 32, 8, 123, 2, 33, 22, 22, 33, -1, -3};
print_array(&second[0]);
return(0);
}
void print_array(int a[])
{
int i;
for(i=0;i<*a;i++)
{
printf("%d ",a[i]);
}
}

Pass a second argument to your function that takes the length of the array. For example:
print_array(int *array, int length)
{
for (int i = 0; i < length; i++) { /* whatever */ }
}

The function has no way of knowing when the array ends. This piece of data simply does not exist unless you pass it manually. The array is just a sequence of bytes in the memory, it has no end delimiter. So you should add a parameter to the function telling it the length of the array.
Yep, this is how it works in C.

Change the function to:
void print_array(int a[], size_t a_size) {
int i;
for(i=0; i< a_size;i++)
// ...
And change the calling of the function to pass in the size:
print_array(second, sizeof(second)/sizeof(second[0]));
Which will calculate the memory size of the array (for a 4 int array on a 32 bit system it'll be 16) and divide it by the size of an int (on a 32 bit system, it's 4 bytes).

in C you can make it with a function and macro:
void printArray_(int *a, int len) {
for (int i = 0; i < len; i++) printf("%d ", a[i]);
}
#define printArray(arr) printArray_((arr), sizeof(arr)/sizeof(arr[0]))
int main(int argc, _TCHAR* argv[])
{
int data[] = { 1,2,3,4 };
printArray(data);
return 0;
}
output:
1 2 3 4

Change this line
print_array(&second[0]);
To
print_array(&second);
Because,
&second[0] just passes the reference to the element at 0th position,which will not be able to traverse the array.
And we cannot traverse the array passed by reference without the size.As there are arrays of varied size, we can compute the size of the array by,
int array_length = sizeof(array)/sizeof(array[0]);
Change the line
void print_array(int a[])
To
void print_array(int *a,int array_length)
And the function of array printing will be as,
void print_array(int *a,int array_length){
int i;
for(i=0;i<array_length;i++){
printf("%d ",*a);
a++; //for incrementing the position of array.
}
}

Related

Array gets modified not int when called in a function

When I run this program-
#include <stdio.h>
void inc( int num[], int n)
{
int i;
n++;
for(i=0;i<10;i++)
num[i]++;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int a=2;
inc (arr, a);
int i;
for(i=0;i<10;i++)
printf("%d ", arr[i]);
printf("\n%d ", a);
return 0;
}
I get the output-
2 3 4 5 6 7 8 9 10 1
2
I understand why the int is unchanged but I don't understand why the array is getting changed since I have not used pointers to call the array. I know that the function will make a different copy of n and assign n=a and all changes will happen to n only and a will be unchanged. Why the array is getting changed?
In C, except for a few cases, an array name decays (=gets implicitly converted) to a pointer to its first element.
This
void inc(int num[], int n)
is exactly same as this:
void inc(int *num, int n)

How do I read this complex C declaration?

I am new to C and am having a problem reading this matrix declaration in a function.
void foo(int (*array)[][3], int row, int col)
and in the function how do I access an array element - say to print it's value.
int (*array)[][3]
declares array to be a pointer to a 2D array whose second dimension is 3. Example usage:
#include <stdio.h>
void foo(int (*array)[][3], int row, int col)
{
printf("%d\n", (*array)[row][col]);
}
int main()
{
int array[10][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
foo(&array, 2, 2);
return 0;
}
Output:
9
(*array)[][3] is a pointer to 2D array.it can point to an int array of variable rows and 3 columns.Here is an example:
int main(void)
{
int arr[3][3] =
{
{0,0,0},
{1,0,0},
{1,1,0},
};
int (*array)[3][3],row,col;
array = &arr;
printf("arr[1][0] : %d\n",(*array)[1][0]);
}

print array in reverse and then add it

Hey guys can you please help me this is the first time I am dealing with arrays and I am tired of trying to solve this question
the task is:
I have 3 arrays and I have to first print them in order and then print them in reverse order using void functions containing a for loop.
the code is:
#include <stdio.h>
void print_array(int numbers[], int length)
{
//insert code here
}
void print_array_reversed(int numbers[], int length)
{
//insert code here
}
int main( int argc, char* argv[])
{
int data_array_1 = ( 1, 3, 5, 7, 9, 11};
int data_array_2 = ( 2, -4, 6, -8, 10, -12, 14, -16};
int data_array_3 = ( 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ,0};
print_array(data_array_1, 6);
print_array_reversed(data_array_1, 6);
print_array(data_array_2, 8);
print_array_reversed(data_array_2, 8);
print_array(data_array_3, 11);
print_array_reversed(data_array_3, 11);
return 0;
}
Please help me guys I am really struggling with this and I have not put anything in the insert code section because nothingIi have come up with has made any sense
NOTE: This isn't an assignment, i am practicing some sample questions to help me understand arrays.however Please help me as my progress has clearly hit a wall.
Check the sample Code.(Print Inorder and Reverse Order)
I think, from this you will get idea how to deal with array and for loop. Keep practice.
#include <stdio.h>
void print_array(int numbers[], int length)
{
printf("In order\n");
for (int i = 0; i < length; i ++)
printf("%d ",numbers[i]);
printf("\n");
}
void print_array_reversed(int numbers[], int length)
{
printf("Reverse order\n");
for(int i = length-1; i >=0; i --)
printf("%d ",numbers[i]);
printf("\n");
}
int main( int argc, char* argv[])
{
int data_array_1[] = { 1, 3, 5, 7, 9, 11};
int data_array_2[] = { 2, -4, 6, -8, 10, -12, 14, -16};
int data_array_3[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ,0};
print_array(data_array_1, 6);
print_array_reversed(data_array_1, 6);
print_array(data_array_2, 8);
print_array_reversed(data_array_2, 8);
print_array(data_array_3, 11);
print_array_reversed(data_array_3, 11);
return 0;
}
Answering entirely is just doing someone else's homework, but....
It's clear you're lost, so let's agree to resolve the question into how to handle some obvious problems from what you have, to get you moving in the right direction.
This must be changed:
int data_array_1 = ( 1, 3, 5, 7, 9, 11};
To:
int data_array_1[] = { 1, 3, 5, 7, 9, 11};
Now, you actually have an array.
Repeat with the other similar occurrences. Take care with the 3rd one, it's a duplicate name. You probably meant data_array_3.
Those changes will move you to a compilable example.
To move you from there, let's just review this one fact:
Given:
void print_array(int numbers[], int length)
As a function signature, within the function numbers[2] would be the 3rd int in the array (they start at zero). If length is 6, the last valid entry is numbers[5]. You can work backwards indexing from 5 to 0.
Let's see how that moves you forward.

How to determine number of elements that are in a array in C

i'm new learning the C Languange.
I want to know the number of elements that are in a array for example
int MyArray[80] = {50, 845, 584};
n = sizeof(MyArray)/sizeof(MyArray[0]); // This will give me 80.
But I only want the number of elements that are inside of {}, As i initialized above it has 3 elements (50, 845, 584), so how do i count them by code?
Another question:
How do i create an empty array so i can add elements by myself and print all elements with a for loop, I've tried this:
int i;
int MyArray[80] = {};
MyArray[0] = 50;
MyArray[1] = 584;
MyArray[2] = 784;
for(i=0; i<=sizeof(MyArray); i++){
printf("Array Element[%d] is: %d", i, MyArray[i]);
}
But this doesn't seem to work, any help? Thanks
EDIT: i fixed it by doing the following code found in another post:
#include <stdio.h>
#define NUM_ELEMS(a) (sizeof(a)/sizeof 0[a])
main() { int i; int numbers[] = {2, 3, 5, 7, 11, 13, 17, 19, 85};
int AllElements = NUM_ELEMS(numbers);
for(i=0; i<AllElements; i++){
printf("Element[%d] in array is: %d \n", i, numbers[i]);
}
}
If you initialize an array with fewer elements than the array is declared to hold, the remaining elements will be default-initialized. For integers, this means that they will be zeroed.
Assuming you have no zeroes in your initialization list, then, you can check how long the initialization list was by checking for the first zero in the array (up to the length of the array).
Alternately, if the array size should just be constant, you should specify the size as [], which makes it exactly as long as the initialization list and no longer.
I only want the number of elements that are inside of {}
Try
size_t n = sizeof (int[]){50, 845, 584} / sizeof (int);
An ugly way using the preprocessor:
#include <stdio.h>
#define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define NARGS(...) NARGS_SEQ(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
#define init_array(arr, ...) NARGS(__VA_ARGS__); arr = {__VA_ARGS__}
int main(void)
{
int n = init_array(int my_array[80], 50, 845, 584);
printf("%d\n", n);
return 0;
}
Is expanded to:
int main(void)
{
int n = 3; int my_array[80] = {50, 845, 584};
printf("%d\n", n);
return 0;
}
Output:
3
Note that this version is limited to N elements in the initializer.

Passing arrays to a void function

I am trying to print out the contents of array1 times 10 if the integer inside is positive, if it is negative , the number should be left as it is in an another array. My code doesn't give me any result, I'm not that good in programming so I'm pretty sure I'm making a stupid mistake but I can't make out what it is, can someone point out the problem
void
tenfold (int array2[], int size )
{
int i, array1[size];
for (i=0;i<size;i++)
{
if (array1[i]>0)
array2[i]= 10 * array1[i];
else
array2[i]= array1[i];
}
}
int main()
{
int i,array1[9]= {3, 4, 5, 6, 7, -8, -9, 1, 2};
int size = 9;
tenfold(array1, size);
return 0;
}
tenfold should take array1 as input and array2 as local, other wise your are correct,
added a printf in tenfold to see the output.
#include<stdio.h>
void tenfold (int array1[], int size )
{
int i, array2[size];
for (i=0;i<size;i++)
{
if (array1[i]>0)
array2[i]= 10 * array1[i];
else
array2[i]= array1[i];
}
for(i=0;i<size;i++)
printf("(orignal) array1[%d]=%d, (new) array2[%d]=%d\n", i, array1[i], i, array2[i]);
}
int main()
{
int i,array1[9]= {3, 4, 5, 6, 7, -8, -9, 1, 2};
int size = 9;
tenfold(array1, size);
return 0;
}
Output:
(orignal) array1[0]=3, (new) array2[0]=30
(orignal) array1[1]=4, (new) array2[1]=40
(orignal) array1[2]=5, (new) array2[2]=50
(orignal) array1[3]=6, (new) array2[3]=60
(orignal) array1[4]=7, (new) array2[4]=70
(orignal) array1[5]=-8, (new) array2[5]=-8
(orignal) array1[6]=-9, (new) array2[6]=-9
(orignal) array1[7]=1, (new) array2[7]=10
(orignal) array1[8]=2, (new) array2[8]=20
You have changed the names of array in the function, update code to
//------------v array1
tenfold (int array1[], int size )
{
int i, array2[size]; //here array2
//your code
}
With your code, array1[] is never initialized and has random values, so if will not work as expected.
You should first start by trying to printf the values in tenfold(). :-)
You don't need to create array1 in tenfold().
Also, you may be able to get away from using a size variable and just use sizeof(array)/sizeof(array[0]).
tenfold doesn't print anything. There is no need to create another array if all you want if for that function to print. Be sure to add #include
#include <stdio.h>
void tenfold (int array[], int size)
{
int i;
for (i=0;i<size;i++)
{
if (array[i]>0)
printf("%d ", (10 * array[i]));
else
printf("%d ", array[i]);
}
}
output:
30 40 50 60 70 -8 -9 10 20

Resources