Properly copying an array from one function to another - c

my C question is how to properly take the binary array I get in binaryConversion() and copy it to integer.binary, within makeInt().
I tried to make binaryConversion() return a pointer which then would be dereferenced and copied over to the new array, but I am almost certain this is wrong in some way.
This is my failed attempt:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
struct Integer{
int decimal;
int binary[32];
};
int * binaryConversion(int num){
int bin_buffer[32];
int * ptr = bin_buffer;
unsigned int mask = INT_MIN;
for(int i = 0; i < 32; i++){
if(mask & num == 1){
bin_buffer[i] = 1;
}
else{
bin_buffer[i] = 0;
}
mask >>= 1;
}
return ptr;
}
struct Integer makeInt(int num){
struct Integer integer;
int * ptr = binaryConversion(num);
for(int i = 0; i < 32; i++){
integer.binary[i] = *(ptr+i);
}
}
and it must work with this driver code:
struct Integer makeInt(int);
void binaryTester(int array[], int test[], int size){
int i;
for(i = 0; i < size; i++){
assert(array[i] == test[i]);
}
printf("\n");
}
int main(){
int size = sizeof(int) * 8;
int array[size], i;
struct Integer test;
printf("\n\t=========Test #7: Conversion of 2===========\n\n");
test = makeInt(2);
int test1[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
printf("\n\t\t....Converting 2 Passed\n");
}
Thanks

Related

From binary to string

I have to convert the binary to string, I tried something, but it didn't work.
This is my code (I don't allowed to change the main function!) , but I can't understand what's wrong:
#include <stdio.h>
#include <stdbool.h>
#include<math.h>
#include<string.h>
void decode_bytes(const int rows, bool bytes[rows][8], char string[rows]){
int iteration = 8;
for(int j = 0; j < rows; j++){
for (int i = 0; i < iteration; i++){
if (bytes[j][i] == 1){
string[j] += pow(2, iteration - (i + 1));
}
}
}
}
int main(){
bool bytes2[7][8] = {
{0,1,0,0,1,0,0,0},
{0,1,1,0,0,1,0,1},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,1,1},
{0,0,1,0,0,0,0,1},
{0,0,0,0,0,0,0,0}
};
char string[7];
decode_bytes(7, bytes2, string);
printf("%s\n", string);
}
Output must be: "hello", but it's "cel<mB"....

error: request for member 'length' in something not a structure or union

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct a{
int length;
};
static int a2(int a[]){
int y = 0;
int x = 0;
for (int i=0; i<a.length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a));
return 0;
}
when I run this code I receive the following error "error: request for member 'length' in something, not a structure or union" can anyone help me to understand the error and how to rectify the code? Thanks
The name of structure and the name of variables are not related.
The argument a is a pointer (int a[] in function arguments has the same meaning as int* a) and it doesn't have members.
You have to pass the length of array to pass to functions aside from (the pointer to the first element of) the array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int a2(int a[], int a_length){
int y = 0;
int x = 0;
for (int i=0; i<a_length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a, sizeof(a) / sizeof(*a)));
return 0;
}

Function to slice an array or struct in c

I have written a function to slice an array in c but it returns addresses I think. I did work correctly once but I screwed up somewhere, any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
int slice_array(int *input_array, int *sliced_array, int n){
int i;
for(i=0; i < n; i++) {
sliced_array[i] = input_array[i];
return 0;
}
};
struct nbrs_ind {
float value;
int index;
};
//I also want to write a function which can slice the members of struct nbrs_ind (i.e. just slice first n indices nbrs_ind)
int main () {
int k=4;
int i;
int a[7] = {1,2,3,4,6,5,7};
int *ptr_a = a;
int b[k];
int *ptr_b = b;
slice_array(a,b,k);
for(i=0;i<k;i++) {
printf("%d\t",b[i]);
}
printf("\n");
}
~
Update, slicing the array works fine but I want to so the same with struct so far I have written the following code. I get he following error:
assignment from incompatible pointer type [enabled by default]
ptr_A = &A;
#include <stdio.h>
#include <stdlib.h>
//This function works fine in drivers program
void slice_array(int *input_array, int *sliced_array, int n){
int i;
for(i=0; i < n; i++) {
sliced_array[i] = input_array[i];
}
};
struct nbrs_ind {
float value;
int index;
};
//Need to make the slice of nbrs_ind struct using following function.
void slice_struct(struct nbrs_ind *input_struct, struct nbrs_ind *sliced_struct, int n){
int i;
for(i=0; i < n; i++) {
sliced_struct[i].index = input_struct[i].index;
sliced_struct[i].value = input_struct[i].value;
}
};
int main () {
int k=3;
int i;
int a[7] = {1,2,3,4,6,5,7};
float c[7] = {2.3,10,3,5,6.4,7.3,1};
int *ptr_a = a;
int b[k];
int *ptr_b = b;
struct nbrs_ind A[7]; // Declare 7 struct of nbrs_ind
//Initilize the delare structs
for (i=0;i<7;i++){
A[i].index = i;
A[i].value = c[i];
}
//How do I make a pointer to the struct so I can pass it to slice_struct function
// I need to be able to do something like slice_struct(A,B,n);
struct nbrs_ind *ptr_A ;
ptr_A = &A;
slice_array(a,b,k);
for(i=0;i<k;i++) {
printf("%d\t",b[i]);
}
printf("\n");
}
~
~
~
#include <stdio.h>
#include <stdlib.h>
int slice_array(int *input_array, int *sliced_array, int n){
int i;
for(i=0; i < n; i++) {
sliced_array[i] = input_array[i];
}
return 0;
};
struct nbrs_ind {
float value;
int index;
};
//I also want to write a function which can slice the members of struct nbrs_ind (i.e. just slice first n indices nbrs_ind)
int main () {
int k=4;
int i;
int a[7] = {1,2,3,4,6,5,7};
int *ptr_a = a;
int b[k];
int *ptr_b = b;
slice_array(a,b,k);
for(i=0;i<k;i++) {
printf("%d\t",b[i]);
}
printf("\n");
}

segfault when trying to access structure; compile option - gcc -std=c99

I am trying to access a structure with a pointer to an integer , from main. But the program crashes. It needs to be built with "std=c99" option as it is the requirement in a test.
The code is as follows:
#include <stdio.h>
#include <malloc.h>
struct Results{
int *A;
int N;
};
struct Results solution(int A[], int N, int K) {
struct Results result;
// write your code in C99 (gcc 4.8.2)
int* T = (int*) malloc(N*sizeof(int));
result.A = A;
result.N = N;
int count = 0;
while(count < K)
{
for(int i = 0; i < N; i++)
{
if(i > 0)
{
T[i] = A[i-1];
}
else
{
T[0] = A[N-1];
}
}
count++;
for(int i = 0; i < N; i++)
{
A[i] = T[i];
}
};
for(int i = 0;i < N; i++)
{
A[i] = T[i];
}
return result;
}
struct Results solution(int A[], int N, int K);
void main()
{
int B[5] = {3,8,9,7,6};
struct Results st;
solution(B,sizeof(B),1);
}
The trouble is at line:
" solution(B,sizeof(B),1);"
What am I doing wrong?
Please help.
You see sizeof(B) would give the number of elements in B times the size of an int, use sizeof(B) / sizeof(B[0]) instead.

How to ignore/remove leading zeros?

I am writing a program to add two large numbers in C.
My integer array result holds the sum of the two numbers (which were also stored in arrays).
For example, if the result array is [0,0,3,2] (actual array size is 20)
If 32 is my actual result, how can I display the contents of the result array without the leading zeros ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BASE 10
void align(int A[],int n);
void add(int A[],int B[], int C[]);
void Invert(int* a, int n);
int main(int argc, char** argv){
char input1[20];
char input2[20];
int size = 20;
int a;
int b;
int num1[20];
int num2[20];
int result[20];
int length1 = strlen(argv[1]);
int length2 = strlen(argv[2]);
int i = 0;
for (i=0;i<length1;i++){
input1[i] = argv[1][i];
}
for (i=0;i<length2;i++){
input2[i] = argv[2][i];
}
a=atoi(input1);
b=atoi(input2);
align(num1,a);
align(num2,b);
add(num1,num2,result);
Invert(result,size);
for (i=0;i<20;i++){
printf("%d",result[i]);
}
return 0;
}
void align (int A[], int n){
int i = 0;
while (n) {
A[i++] = n % BASE;
n /= BASE;
}
while (i < 20) A[i++] = 0;
}
void add (int A[], int B[], int C[]) {
int i, carry, sum;
carry = 0;
for (i=0; i<20; i++) {
sum = A[i] + B[i] + carry;
if (sum >= BASE) {
carry = 1;
sum -= BASE;
} else
carry = 0;
C[i] = sum;
}
if (carry) printf ("overflow in addition!\n");
}
void Invert(int* a, int n)
{
int i;
int b;
for(i=0; i<n/2; i++){
b = a[i];
a[i] = a[n-i-1];
a[n-i-1] = b;
}
}
`
To get the actual digits (I assume that each digit is stored as a byte in an array of 20 bytes, lowest digit at highest index), you do something like this:
int i;
int size = sizeof(thearray) / sizeof(thearray[0]);
/* find first non-0 byte, starting at the highest "digit" */
for (i = 0; i < size - 1; ++i)
if (thearray[i] != 0)
break;
/* output every byte as character */
for (; i < size; i++)
printf("%c", thearray[i] + '0'); /* 0 --> '0', 1 --> '1', etc. */
printf("\n");
You can do this by below code:-
int flag=1;
for(i=0;i<20;i++)
{
if(flag==1&&array[i]!=0)
flag=0;
if(flag!=1)
{
printf("%d",array[i]);
}
}
This will remove all leading zeros.
I propose a solution by using the pointer. The situation where only zero is stored in the array is also handled. I'm more comfortable with the pointer.
int test[20] = {0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,1,2,3,4,5};
int test_bis[20] = {0};
int * ptr_test = test_bis;
int ii = 0;
while( *(ptr_test)== 0 && ii < 20 ) {
ptr_test++;
ii++;
}
if( ii < 20)
do {
printf("%d",*(ptr_test));
ptr_test++;
} while (++ii < 20);
else
printf("0");
Thats for integer array you can modify it accordingly.
for(i=0;i<20;i++){
if(flag==1&&array[i]==0)
{
// just skips until first nonzero
}
else if(flag==1&&array[i]!=0){
flag=0; // when first nonzero comes set flag to 0 and print it
printf("%d",array[i]);
}
else {
printf("%d",array[i]); // after first nonzero simply print it
}
}

Resources