print array in reverse and then add it - c

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.

Related

I got an error in a function call of an array in C?

I got an unexpected error while doing a function call in C using the array, I know a little bit of C and I don't understand how I can fix it. The code of the main function is attached below.
void sum_matrices (int m1[][NUM_COLS], int m2[][NUM_COLS], int num_rows, int result[][NUM_COLS]);
void print_matrix (int m[][NUM_COLS], int num_rows);
void print_array (int a[], int len);
int main(void) {
int my_matrix_1[][NUM_COLS] = {{1, 5, 3, 4, 2},
{7, 1, 4, 1, 7},
{6, 9, 2, 0, 5}};
int my_matrix_2[][NUM_COLS] = {{7, 8, 3, 3, 4},
{1, 3, 7, 8, 2},
{1, 3, 5, 6, 0}};
int num_rows = 3;
int result[num_rows][NUM_COLS]; // finish this line of code to create the result matrix to pass to sum_matrices
// add call to sum_matrices to add my_matrix_1 and my_matrix_2
sum_matrices (my_matrix_1[][NUM_COLS], my_matrix_2[][NUM_COLS], num_rows, result[][NUM_COLS]);
print_matrix(my_matrix_1, num_rows);
printf("\n");
print_matrix(my_matrix_2, num_rows);
printf("\n");
print_matrix(result, num_rows);
return 0;
}
Your "call" of function sum_matrices within function main...
sum_matrices (my_matrix_1[][NUM_COLS], my_matrix_2[][NUM_COLS], num_rows, result[][NUM_COLS]);
is a mixture of a forward declaration of a function and a function call, but neither of both is complete or allowed at this position in that form.
To call the function, write...
sum_matrices (my_matrix_1, my_matrix_2, num_rows, result);

error: assigning to 'int **' from incompatible type 'int [][] [duplicate]

This question already has answers here:
Is 2d array a double pointer? [duplicate]
(4 answers)
Closed 7 years ago.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define SIZE 9
typedef struct
{
int row;
int col;
int** arr;
}parameters;
// function prototypes
void* checkRow(void* in);
void* checkCol(void* in);
void* checkSect(void* in);
int main(void)
{
// board to test
int grid[SIZE][SIZE] = {
{6, 5, 3 ,1, 2, 8, 7, 9, 4},
{1, 7, 4, 3, 5, 9, 6, 8, 2},
{9, 2, 8, 4, 6, 7, 5, 3, 1},
{2, 8, 6, 5, 1, 4, 3, 7, 9},
{3, 9, 1, 7, 8, 2, 4, 5, 6},
{5, 4, 7, 6, 9, 3, 2, 1, 8},
{8, 6, 5, 2, 3, 1, 9, 4 ,7},
{4, 1, 2, 9, 7, 5, 8, 6, 3},
{7, 3, 9, 8, 4, 6, 1, 2, 5}
};
printf("Sudoku from Khoa Vo\n");
int i = 0;
int j = 0;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
{
printf("%d ", grid[i][j]);
if(j == SIZE)
printf("\n");
}
// paramenter for column and row threads
parameters *data = (parameters*) malloc(sizeof(parameters));
data->row = 0;
data->col = 0;
data->arr = grid;
}
I have an error, but I cant quite figured out how to fix it, there error is on statement data->arr = grid, the error message indicate that `error: assigning to 'int **' from incompatible type 'int [9][9]. Can I get some suggestion on how to fix this error, thank in advance.
If it's for sudoku just change the structure definition1
typedef struct
{
int row;
int col;
int arr[9][9];
} parameters;
Since int ** and int[][] are not compatible types because int[][] stores contigous integers while with int ** you can store contigous pointers but the integers are not contigous you should not force a cast neither.
To fill arr with the values you could then use
memcpy(data->arr, grid, sizeof(data->arr));
because you cannot assign to arrays.
Also, be careful when using malloc(), on error it returns NULL and in that case the code following malloc() will cause undefined behavior. And do not cast void * in [tag:c].
1You could define it as an array of pointers too as this comment suggests..

Decrementing an array from last element in C

I want to decrement an array from last element in C. I first wrote the following code to increment an array from the first element:
#include<stdio.h>
int x[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *pointer, count;
int main (void) {
pointer = x;
for (count = 0; count < 11; count++)
printf("%d\n", *pointer++);
return 0;
}
This works fine. But then I tried to decrement the elements by modifying the code to this:
#include<stdio.h>
int x[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *pointer, count;
int main (void) {
pointer = x[10];
for (count = 0; count < 11; count++)
printf("%d\n", *pointer--);
return 0;
}
But of course I am doing something wrong. I'd appreciate it very much if you could help me to understand my mistake.
You have two options which are equivalent.
pointer = &x[10];
pointer = x + 10;
Either will achieve the effect of making the pointer point at the 10th element of x.
pointer = x[10]; should be pointer = &x[10];.
You're setting pointer to the integer value x[10]. What you want to do is set pointer to the address of the last element in x.

Variadic Adding Function C

I'm writing a simple variadic function that adds together a series of ints and returns the sum. I'm having a bit of trouble understanding how it works and my code doesn't seem to work though I feel I'm in the right direction with my code. (Posted below) The specifications of this function are that it takes at least one param and the last param is always a zero (as seen called in the main). I was also told, based upon my machine, that I wouldn't necessarily get the output I'm looking for which, as you could imagine, further complicates my situation. Some assistance with correcting my Sum() function would be greatly appreciated.
EDIT:
This is supposed to be done w/o the use of stdarg.h header and thus no va_arg functions.
int Sum(int a, ... ) {
int sum = 0, *addy = &a;
while (*addy) {
sum += *addy;
addy += sizeof(a);
}
return sum;
}
int main() {
printf("%d %d %d %d\n", Sum(0), Sum(3, 5, 6, 7, 0),
Sum(7, 2, 42, 3, 5, -4, 0), Sum(-1, 9, 12, 123, -213, 42, 7, 2, 0));
}
//Expected output: 0 21 55 -19
//My output: 0 32770 32770 32776
When you add a number to an int pointer (as in addy += sizeof(a)) the number you add is automatically multiplied by the size of whatever type the pointer is declared as (in this case int). To fix this, just use
addy += 1;
instead. However, I would recommend using variadic macros instead of this method, they are clearer and less error prone.
for variable arguments, you have to use va_start and va_end functions, hope useful..
http://www.gnu.org/software/libc/manual/html_node/Variadic-Example.html#Variadic-Example
Can you please check this
int Sum(int a, ... ) {
int sum = 0, *addy = &a;
while (*addy) {
sum += *addy;
addy ++;
}
return sum;
}
int main() {
printf("%d %d %d %d\n", Sum(0), Sum(3, 5, 6, 7, 0),
Sum(7, 2, 42, 3, 5, -4, 0), Sum(-1, 9, 12, 123, -213, 42, 7, 2, 0));
}
Point to remember is for pointer operations: the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. So incrementing the pointer addy is enough for geting the next element.
#include <stdarg.h>
#include <stdio.h>
int
add_em_up (int count,...)
{
va_list ap;
int i, sum;
va_start (ap, count); /* Initialize the argument list. */
sum = 0;
for (i = 0; i < count; i++)
sum += va_arg (ap, int); /* Get the next argument value. */
va_end (ap); /* Clean up. */
return sum;
}
int
main (void)
{
/* This call prints 16. */
printf ("%d\n", add_em_up (3, 5, 5, 6));
/* This call prints 55. */
printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
return 0;
}

Printing Arrays in separate Function in 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.
}
}

Resources