For my studies, I have to write a C function GetEvenNumber:
parameters: array with n integers + array size;
returns tr array which contains even integers from td.
I don't know a priori the length of the array tr.
My below code returns errors:
#include <stdio.h> // define the header file
int *GetEvenNumber(int t[], int size)
{
int tr[];
int j = 0;
for (int i = 0; i < size; i++)
{
if (t[i] % 2 == 0)
{
printf("%d is even \n", t[i]);
tr[j] = t[i];
j++;
}
}
return tr;
}
int main() // define the main function
{
int *t; // = {4, 3, 1, 8, 6 };
int *tr = GetEvenNumber(t, 5);
for (int i = 0; i < 5; i++)
printf("%d \n", tr[i]);
}
I get error:
error: array size missing in 'tr'
int tr[];
warning: function returns address of local variable [-Wreturn-local-addr]
return tr;
How do I fix that? Thanks.
You mentioned that you could not use malloc() to dynamically create tr within GetEvenNumber() to address the two issues raised by your copmiler. This leaves making tr a global variable, or as here pass in the result array tr to be filled out:
#include <stdio.h>
#include <stdlib.h>
void GetEvenNumber(size_t size, const int *td, size_t *size2, int *tr) {
*size2 = 0;
for(size_t i=0; i<size; i++)
if(td[i] % 2 == 0)
tr[(*size2)++] = td[i];
}
int main() {
int td[] = {4, 3, 1, 8, 6 };
size_t size = sizeof(td) / sizeof(*td);
int tr[size];
size_t size2;
GetEvenNumber(size, td, &size2, tr);
for (size_t i=0; i < size2; i++)
printf("%d \n", tr[i]);
}
If the input array td contains uneven elements, then the result array tr have fewer valid elements than the input. I used size2 here to tell caller how many elements are valid in tr. Your code did not assign any values to, in this example, last 3 elements. You don't tell us what should happen with those last elements.
In modern C, if you specify the size before the array in the argument, then you can use the size in array specification which help document what is going on.
The error is due to
int tr[];
because you have to specify the size of your array during its creation.
I suggest trying to add a function that returns the number of even numbers in the array:
int getEvenNum(int t[], int lent){
int numEven = 0; // initialize counter to zero
for(int i = 0; i < lent; i++){ // for each element of the array
if ((t[i] % 2) == 0){ // if it's even,
numEven++; // add 1 to counter
}
}
return(numEven); // finaly returns the value of the counter
}
and then you replace the int tr[]; by int tr[getEvenNum(t, size)]; (maybe there's a ; after the getEvenNum(t, size) but I'm not sure)
Since the array tr can have AT MOST the same number of elements as the original integer array, it would be safe to declare the array with the same size as the array 't[]'.
I have made some changes to your code. Try the following:
#include<stdio.h> // define the header file
void GetEvenNumber(int *t, int* tr, int size, int *pCountEven)
{
int i, j=0;
for (i=0; i < size; i++)
{
if(t[i]%2==0)
{
printf("%d is even \n", t[i]);
tr[j++] = t[i];
}
}
*pCountEven = j;
}
int main() // define the main function
{
int t[] = {4, 3, 1, 8, 6 };
int tr[5], countEven = 0, i;
GetEvenNumber(t, tr, 5, &countEven);
for (i=0; i < countEven; i++)
printf("%d\n", tr[i]);
return 0;
}
Edit: As #chqrlie (who is an experienced coder) pointed out, we can simply return the length of the array instead of taking the address of a variable.
So alternatively, you can try this:
#include <stdio.h> // define the header file
int GetEvenNumber(int *t, int *tr, int size) {
int i, j = 0;
for (i = 0; i < size; i++) {
if (t[i] % 2 == 0) {
printf("%d is even \n", t[i]);
tr[j++] = t[i];
}
}
return j;
}
int main() // define the main function
{
int t[] = { 4, 3, 1, 8, 6 };
int tr[5], countEven = 0, i;
countEven = GetEvenNumber(t, tr, 5);
for (i = 0; i < countEven; i++)
printf("%d\n", tr[i]);
return 0;
}
Related
With my programm I try to change the order of numbers in the int array. To the first function, I just passed both arrays and printed the array called arraytemp with the changed order. After that I printed in the main function the same array, just to see if the array was filled too. I havented used any pointers in the first function - how did the array got filled? Does the arrays adress get passed to functions anyway?
Then I wanted to pass arrays with the same content to the second function, but this time I used pointers. I have no clue, how to get the same result printed, because I get a stack smashing error. I am kinda comfused with '*' and '&'. So, how should I pass these arrays when using pointers?
#include <stdio.h>
void switchnum (int arraytemp[6], int array[], int laenge) {
printf("\n\nAfter (in function 1):\n");
for(int i = 0 ; i<laenge ; i++) {
arraytemp[i] = array[laenge-1-i];
printf("%d ", arraytemp[i]);
}
return 0;
}
void switchnum2 (int *arraytemp2[6], int array2[], int laenge2) {
printf("\nAfter (in function2):\n");
for(int j = 0 ; j<laenge2 ; j++) {
arraytemp2[j] = array2[laenge2-1-j];
printf("%d ", arraytemp2[j]);
}
return 0;
}
int main() {
int array[] = {4,8,1,3,0,9};
int arraytemp[6];
printf("Before (main):\n");
for(int i = 0 ; i<6 ; i++) {
printf("%d ", array[i]);
}
switchnum(arraytemp, array, 6);
printf("\nAfter (in main):\n");
for(int i = 0 ; i<6 ; i++) {
printf("%d ", arraytemp[i]);
}
int array2[] = {4,8,1,3,0,9};
int arraytemp2[6];
switchnum2(arraytemp2, array2, 6);
return 0;
}
The compiler adjusts a parameter having an array type to pointer to the array element type.
So this function declaration
void switchnum (int arraytemp[6], int array[], int laenge);
is equivalent to the following declaration
void switchnum (int arraytemp[], int array[], int laenge);
and the same way is equivalent to the following declaration
void switchnum (int *arraytemp, int *array, int laenge);
As for this function declaration
void switchnum2 (int *arraytemp2[6], int array2[], int laenge2);
then it is adjusted by the compiler to the declaration
void switchnum2 (int **arraytemp2, int *array2, int laenge2);
So the used argument expression and the function parameter have incompatible pointer types.
Pay attention to that in this call
switchnum(arraytemp, array, 6);
the both arrays are converted to pointers to their first elements of the type int *.
In fact this call is equivalent to
switchnum( &arraytemp[0], &array[0], 6);
How to pass pointer of arrays to functions
In this case you are trying to pass an array of pointers to a function and not a pointer of arrays:
void switchnum2(
int* arraytemp2[6], int array2[], int laenge2)
Maybe it helps to see the output of this example program, as it shows the output of many of your cases
Example
#include <stdio.h>
void test_arr(int*[6]);
int main(void)
{
int array[] = {4, 8, 1, 3, 0, 9};
int* pArr[6] = {0}; // 6 pointers to int
printf("original vector in main(): ");
for (int i = 0; i < 6; i += 1) printf("%d ", array[i]);
printf("\n");
for (int i = 0; i < 6; i++) pArr[i] = &array[i];
test_arr(pArr);
printf("\nIn main() &array[0] = %p\n", &array[0]);
return 0;
}
void test_arr(int* pInt[6])
{
printf("In test_array(): ");
for (int i = 0; i < 6; i += 1)
printf("%d ", *pInt[i]);
printf("\n");
int* myP = *pInt;
printf("*pInt\tpoints to value %d\n", *myP);
myP = pInt[0];
printf("pInt[0]\tpoints to value %d\n", *myP);
int x = *pInt[0];
printf("*pInt[0] = %d\n", x);
printf("\ntest_array() &pInt[0] = %X\n", pInt[0]);
return;
}
output
original vector in main(): 4 8 1 3 0 9
In test_array(): 4 8 1 3 0 9
*pInt points to value 4
pInt[0] points to value 4
*pInt[0] = 4
test_array() &pInt[0] = 197BFB00
In main() &array[0] = 000000CF197BFB00
Your program with some changes in the functions
I changed some lines in your code to get the expected result
#include <stdio.h>
void switch1(const int[],int[],const int);
void switch2(const int[],int*[],const int);
void show_array(const int[6],const char*);
int main(void)
{
int arr_out[] = {0,0,0,0,0,0};
show_array(arr_out, "arr_out in main()");
// call 1st function
printf("switch1() uses int[] as output\n");
switch1((int[6]){6, 5, 4, 3, 2, 1}, arr_out, 6);
show_array(arr_out, "arr_out using 6..1 array as input and 1st function");
// for 2nd function we need an array of pointers
int* pArr[6] = {0}; // 6 pointers to int
for (int i = 0; i < 6; i++) pArr[i] = &arr_out[i];
printf("switch2() uses int*[] as output\n");
switch2((int[6]){1, 2, 3, 4, 5, 6}, pArr, 6);
show_array(arr_out, "arr_out using 1..6 array as input and 2nd function");
return 0;
}
void switch1(const int in[], int out[], const int laenge)
{
for (int i = 0; i < laenge; i++)
out[i] = in[laenge - 1 - i];
}
void switch2(const int in[], int* out[], const int laenge)
{
for (int i = 0; i < laenge; i++)
*out[i] = in[laenge - 1 - i];
}
void show_array(const int array[6], const char* msg)
{
printf("%s:\t", msg);
for (int i = 0; i < 6; i++) printf("%d ", array[i]);
printf("\n");
}
output of the modified code
arr_out in main(): 0 0 0 0 0 0
switch1() uses int[] as output
arr_out using 6..1 array as input and 1st function: 1 2 3 4 5 6
switch2() uses int*[] as output
arr_out using 1..6 array as input and 2nd function: 6 5 4 3 2 1
about the changes
void show_array(const int array[6], const char* msg)
{
printf("%s:\t", msg);
for (int i = 0; i < 6; i++) printf("%d ", array[i]);
printf("\n");
}
This function is a helper to show the array contents and accepts a title. Very convenient here
the 2 functions has no output (printf() calls)
parameters are declared const so we can build the vector at the function call
I am using shorter names and changed the order of arguments to input and then output
void switch1(const int in[], int out[], const int laenge)
{
for (int i = 0; i < laenge; i++)
out[i] = in[laenge - 1 - i];
}
void switch2(const int in[], int* out[], const int laenge)
{
for (int i = 0; i < laenge; i++)
*out[i] = in[laenge - 1 - i];
}
Here you see the difference between the 2 functions: a single asterisk.
But in order of using the second function you need to build the vector of pointers as here
// call 1st function
printf("switch1() uses int[] as output\n");
switch1((int[6]){6, 5, 4, 3, 2, 1}, arr_out, 6);
show_array(arr_out, "arr_out using 6..1 array as input and 1st function");
// for 2nd function we need an array of pointers
int* pArr[6] = {0}; // 6 pointers to int
for (int i = 0; i < 6; i++) pArr[i] = &arr_out[i];
printf("switch2() uses int*[] as output\n");
switch2((int[6]){1, 2, 3, 4, 5, 6}, pArr, 6);
show_array(arr_out, "arr_out using 1..6 array as input and 2nd function");
The task is as follows: to fill the file with numbers using the generation() function - an array cannot be used here. Then read the digits into the array by the read_file() function into the array and sort it using the quick sort method. Output to the console.
I wrote the code as I logically understood it myself, but a set of values is output to the console after sorting, as it is output in case of an error with a lack of memory, random numbers with cons sometimes. Maybe it's worth filling in a dynamic array? And why, if I know a specific number of elements?
Please help me figure it out.
#include <stdio.h>
#include <stdlib.h>
#define N 10
#define A -25000
#define B 25000
void generation();
void read_file();
void method_simple_sort();
int main(void) {
generation();
method_simple_sort();
return EXIT_SUCCESS;
}
void generation() {
FILE *file;
int i;
file = fopen("index.txt", "w");
srand(time(0));
for (int i = 0; i < N; i++) {
fprintf(file, "%d\n", A + rand() % (A - B + 1));
}
fclose(file);
}
void read_file() {
FILE *file;
int i, a[N] = { 0 };
file = fopen("index.txt", "r");
for (i = 0; i < N; i++) {
fscanf(file, "%d ", &a[i]);
}
fclose(file);
for (int i = 0; i < N; i++)
printf("%d ", a[i]);
}
void method_simple_sort() {
int a[N], i = 0, k = 0, ind_max = 0, temp = 0;
read_file();
for (k = 0; k < N - 1; k++) {
ind_max = k;
for (i = 1 + k; i < N; i++) {
if (a[i] > a[ind_max]) {
ind_max = i;
}
}
temp = a[k];
a[k] = a[ind_max];
a[ind_max] = temp;
}
// вывод результатов в файл
printf("\n\nПростого выбора: ");
for (int i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n\n\n\n");
}
The function method_simple_sort defines a local array variable int a[N].
It then calls read_file(), but read_file() does not fill this local array variable defined in method_simple_sort.
Instead it fill its own local array variable (called also int a[N], but it's a different one than the one in method_simple_sort).
The bottom line is that when method_simple_sort attempts to sort a it sorts an array containing uninitialized data.
Hence the "garbaged" values that you see printed at the end.
The array used by the methods read_file and method_simple_sort aren't the same. When you declare anything inside a function it is translated at low level in space to allocate on the stack when you call the function.
You should transform read_file(void) into read_file(int *a) without declaring another instance of an array inside read_file.
By doing that you can pass a reference to the array declared inside method_simple_sort.
In particular:
void read_file(int *a) {
FILE *file;
int i;
/* a[N] = { 0 };*/
...
}
void method_simple_sort() {
int a[N], i = 0, k = 0, ind_max = 0, temp = 0;
read_file(a);
...
}
I'm writing a C for which I need to create a 2D array. I've found a solution to my problem using double pointers (pointers to pointers) in the following way:
#include <stdio.h>
#include <stdlib.h>
int d = 3;
#define DIM_MAX 9
void changeArray(int d, int *array[d]);
int main()
{
//alocate array of 'd' colummns and 'd' row using malloc using array of pointers
int **array = malloc(d*sizeof(int *));
for(int count = 0; count < d; count++)
{
array[count] = malloc(d*sizeof(int *));
}
/* Call changeArray function */
changeArray(d, array);
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
for(int count = 0; count < d; count++)
{
free(array[count]);
}
return 0;
}
void changeArray(int n, int *array[d])
{
for(int i =0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
array[i][j] = i*j;
}
}
return;
}
The code above works pretty well (it seems), but I've read in the web that using pointer to pointer is not the correct way to create 2D arrays. So I've come up with the following code, which also works:
#include <stdio.h>
#include <stdlib.h>
#define DIM_MAX 9
int d = 3;
void changeArray(int d, int *array[d]);
int main()
{
//alocate array of 'd' colummns and 'd' row using malloc using array of pointers
int *array[DIM_MAX] = {0};
for(int count = 0; count < d; count++)
{
array[count] = (int *)malloc(d*sizeof(int *));
}
/* Call changeArray function */
changeArray(d, array);
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
for(int count = 0; count < d; count++)
{
free(array[count]);
}
return 0;
}
void changeArray(int n, int *array[d])
{
for(int i =0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
array[i][j] = i*j;
}
}
return;
}
What is the difference in using any of the two ways above to write this code?
[Not an answer, but an alternative approach to achieve the desired result, namely defining a user-defined 2D array.]
Assuming the compiler in use supports VLAs you could do this:
#include <stddef.h> /* for size_t */
void init_a(size_t x, size_t y, int a[x][y]); /* Order matters here!
1st give the dimensions, then the array. */
{
for (size_t i = 0; i < x; ++i)
{
for (size_t j = 0; j < y; ++j)
{
a[i][j] = (int) (i * j); /* or whatever values you need ... */
}
}
}
int main(void)
{
size_t x, y;
/* Read in x and y from where ever ... */
{
int a[x][y]; /* Define array of user specified size. */
init_a(x, y, a); /* "Initialise" the array's elements. */
...
}
}
It is actually pretty simple. All you have to do is this:
int i[][];
You are overthinking it. Same as a normal array, but has two indexes.
Let's say you want to create a "table" of 4 x 4. You will need to malloc space for 4 pointers, first. Each of those index points will contain a pointer which references the location in memory where your [sub] array begins (in this case, let's say the first pointer points to the location in memory where your first of four arrays is). Now this array needs to be malloc for 4 "spaces" (in this case, let's assume of type INT). (so array[0] = the first array) If you wanted to set the values 1, 2, 3, 4 within that array, you'd be specifying array[0][0], array[0][1], array[0][2], array[0][3]. This would then be repeated for the other 3 arrays that create this table.
Hope this helps!
I'm writing a program to print a numbers in array then used function to split the array to an odd and even number. I've done this part, then I have to sent each of the odd and even array to another function that calculate the largest number in each array and return the calculated value. It seem there something missing in the main function.
#include <stdio.h>
void split();
int largest(int arr[]);
int main() {
int a[11] = {2, 4, 5, 6, 7, 1, 8, 3, 9, 14, 17};
printf("the values of the array is :\n");
for (int i = 0; i < 11; i++) {
printf("%d ", a[i]);
}
split();
int z = largest(odd);
printf("the max value in odd array is :%d\n", z);
}
void split() {
int i, evenNumber = 0, oddNumber = 0;
int even[11], odd[11];
int a[11] = {2, 4, 5, 6, 7, 1, 8, 3, 9, 14, 17};
for (i = 0; i < 11; i++) {
if (a[i] % 2 == 0) {
even[evenNumber++] = a[i];
} else {
odd[oddNumber++] = a[i];
}
}
printf("\nEven :\n");
for (i = 0; i < evenNumber; i++) {
printf("%d ", even[i]);
}
printf("\nOdd :\n");
for (i = 0; i < oddNumber; i++) {
printf("%d ", odd[i]);
}
}
int largest(int odd[]) {
int max;
max = odd[0];
for (int i = 0; i < 11; i++) {
if (max < odd[i]) {
max = odd[i];
}
}
return max;
}
You actually can use arrays in other functions. However, the problem you are running into is scope.
The Problem
In your main function,
int main ()
{
int a[11] = {2,4,5,6,7,1,8,3,9,14,17};
printf("the values of the array is :\n");
for (int i = 0 ; i<11 ; i++){
printf("%d ",a[i]);
}
split();
int z = largest(odd);
printf("the max value in odd array is :%d\n",z);
}
the line int z = largest(odd); uses the variable odd. However, odd is never defined in main. Rather, odd is defined in the scope of split().
It appears you think that because you are calling split() in main, that somehow you will able to access odd in main after you call split(). This is not true. Even though odd is defined in split(), it is defined only in split(). So even though main calls split(), odd is still not defined in main.
This is because a variable is limited to the scope in which it is defined. If a variable is defined in a function (i.e. the function is its scope), it is accessible only within that function, even if that function is used elsewhere. If you want to use a variable outside of the function in which it is defined, you must somehow "return" that variable from the function.
I strongly recommend reading a tutorial on how "scope" works in C, e.g. this tutorial.
The Solution
You have a two main options:
Rewrite split() to somehow return odd.
Move the contents of split() to main. I.e. don't make split() a separate function.
Unless you have a specific reason for keeping split() as a separate function, I would strongly recommend just moving its contents into main. Otherwise, you will have to pass multiple parameters as references to make split() work the way you want it to.
If we move the contents of split() into main (and make the necessary adjustments to remove duplicate variables), this is what the new code will look like:
#include <stdio.h>
int largest(int arr[]);
int main() {
int a[11] = { 2, 4, 5, 6, 7, 1, 8, 3, 9, 14, 17 };
printf("the values of the array is :\n");
for (int i = 0; i < 11; i++) {
printf("%d ", a[i]);
}
int evenNumber = 0, oddNumber = 0;
int even[11], odd[11];
for (int i = 0; i < 11; i++) {
if (a[i] % 2 == 0) {
even[evenNumber++] = a[i];
} else {
odd[oddNumber++] = a[i];
}
}
printf("\nEven :\n");
for (int i = 0; i < evenNumber; i++) {
printf("%d ", even[i]);
}
printf("\nOdd :\n");
for (int i = 0; i < oddNumber; i++) {
printf("%d ", odd[i]);
}
int z = largest(odd);
printf("the max value in odd array is :%d\n", z);
}
int largest(int odd[]) {
int max = odd[0];
for (int i = 0; i < 11; i++) {
if (max < odd[i]) {
max = odd[i];
}
}
return max;
}
One more comment:
Whenever you pass an array to a function, you should always pass the length of the array too. For example:
int largest(int odd[], int len) {
int max = odd[0];
for (int i = 0; i < len; i++) {
if (max < odd[i]) {
max = odd[i];
}
}
return max;
}
And then you would call largest(int[], int) like this:
largest(odd, 11);
I want to create a table with keys from an array. Is there an easy way to do it.
int array1[] = {1,5,3,8,9,11};
// table[1]
// table[5]
// table[3]
int count(int a)
{
//a is one of the values in array. array1[] = {1,5,3,8,9,11};
// for ex 3.
// I have to figure out how many times this function was called with what values 1/5/3/8/9/11
table[3]++;
}
A simple code
#include<stdio.h>
int n = 6; //number of elements in array1
int array1[] = {1,3,5,8,9,11};
int *funCount;//Count of elements in array1
int count(int a)
{
int i;
for(i = 0; i < n; i++)
if(a == array1[i])
break;
funCount[i]++;
}
int main()
{
funCount = (int*)calloc(n, sizeof(int));
int i;
count(1);
count(3);
count(5);
count(8);
count(9);
count(11);
for(i = 0; i < n; i++)
printf("%d ",funCount[i]);
return 0;
}
This approach is okay if your array1 is going to be small!
Otherwise I would recommend you to use hashing