I'm writing program but getting this warning !! Can someone help me in this regard.
#include <stdio.h>
#include <stdlib.h>
typedef int bool;
#define true 1
#define false 0
#define M 5 // Define total molecules
#define MAX 31 // used to Create a range
#define N 6 // Define total nodes in the given graph
unsigned int nondet_uint(); // returns a non_deterministic uint
typedef unsigned __CPROVER_bitvector[M] bitvector;
//constrains the non_detrministic uint to be between 0 and n
unsigned int oneTon (unsigned int n){
unsigned int result = nondet_uint();
__CPROVER_assume(result>=1 && result<=n);
return result;
};
//Constrain the value between 0 and 1
unsigned int nondet (){
unsigned int num = nondet_uint();
__CPROVER_assume( num>= 0 && num <= 1);
return num;
};
//Define a structure to represent the EdgeBag that is a tuple :
// edgeBag := (i,j,edgeWeight, mask, inclusionSet, exclusionSet)
struct EdgeBag
{
int ith;
int jth;
bitvector edgeWeight;
bitvector mask;
bitvector inclusionSet;
bitvector exclusionSet;
};
// Setweight function
// Going to give the edges the weight and then allow furture calculation of inclusion and exclusion set
bitvector setWeight( bitvector node) {
bitvector edge;
edge = 00000;
for(unsigned int k=0; k<N; k++){
if ((node & (00001 << k)) == (00001 << k)) {
edge = (edge | (nondet() << k));
}
}
printf("value of the egde is %d", edge);
return edge;
}
void main(){
unsigned int pos , i, j, k, l, v, w, x, y , iVal, jVal;
unsigned int edgePos, bagNo = 0, colorNode = 0 , minColor, len = 0, cPos = 0;
bool C0 , C1 , C2 , C3;
bitvector compartment1 , compartment2 , compartment3 , compartment4, compartment5, compartment6;
bitvector nodes[N] = {compartment1, compartment2, compartment3, compartment4, compartment5, compartment6};
bitvector total , fareTotal, outTotal, inTotal;
// Represent the graph with given topology
bool graph[N][N] = {{0, 0, 1, 1, 0, 0 },
{1, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0}
};
// Specify that all compartments are distinct
for(i=0; i<N; i++) {
for(j=0 ; j < N ; j++) {
if(graph[i][j] == 1) {
len = len + 1;
}
if(i != j) {
__CPROVER_assume(nodes[i] != nodes[j]);
}
}
}
// Create a structure that will contain edges.
struct EdgeBag edgeBag[len];
// Define colorGraph and colorSet for the represetaion
bool colorGraph[len][len];
unsigned int colorSet[len];
// Set the weight of the edge : for each edge in the graph call add to the esdge bag and add its edgeweight
// setWeight function and allow all possible subsets to move from a node to anotheer
// Set ith jth position and edgeweight to the position of edgeBag[len] array
edgePos = 0;
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
if (graph[i][j] == 1) {
edgeBag[edgePos].ith = i;
edgeBag[edgePos].jth = j;
**HERE IS THE PROBLEM !!!**
edgeBag[edgePos].edgeWeight = setWeight(nodes[i]);
edgePos = edgePos + 1;
}
}
}
I understand that this warning comes when compiler has not seen a declaration before function use. But i have declared the setWeight Function already and looks like type too matches. Kindly help me with this , I got stuck here since last night. Thanks
you function expects bitvector - bitvector setWeight( bitvector node)
but gets bitvector[ ] -
bitvector nodes[N] = {...};
...
[edgePos].edgeWeight = setWeight(nodes);
Related
I am trying to convert the scrambler and descrambler to c matlab link, the scrambler part was done as follows:
#include <stdint.h>
#include <stdio.h>
/* Binary scramble
input: array of size *size* to be processed
output: array to store the result, must be as big as input
poly: polynom to use: array of *order* size
state: internal state: array of *order* size
order: order of polynom
*/
void scramble (uint8_t *input, uint8_t *output,size_t size, uint8_t *poly, uint8_t *state, size_t order)
{
/* ignore the order 0 */
poly++;
state++;
order--;
/* for each input bits */
for(int i = 0; i < size; i++)
{
/* compute the value to be xored to input */
uint8_t xor = 0;
for(int n = 0; n < order; n++)
{
if(poly[n])
{
xor ^= state[n];
}
}
/* shift the state */
for (int k = order-1; k > 0; k--)
{
state[k] = state[k-1];
}
/* compute the ouput */
output[i] = state[0] = xor ^ input[i] ;
}
}
int main(void)
{
uint8_t bin[10] = { 1, 0, 0, 1, 0, 0, 0, 1, 1, 0};
uint8_t out[10];
uint8_t poly[5] = {1, 1, 1, 0, 1};
uint8_t state[5] = {0};
scramble(bin, out, 10, poly, state, 5);
for (int i = 0; i < 10; ++i)
{
printf("%d -> %d\n", bin[i], out[i]);
}
return 0;
}
I tried to write the descrambling part as describe in this matlab link, as far as i know to convert it to descrambler i need to invert the xor bit when i assign it to the output as follows output[i] = state[0] = !xor ^ input[i] and also need to reset the state back to 0's before calling a descramble function but I do not get the correct output similar to the input of the scrambler. Should i change something else as well?
I have an array of numbers that has been sorted in before, so there's no need to sort it, I need to insert an given value, named it val, at a valid position in my array.
My program works for a given value that is smaller than the last one, but for the case where the value is bigger than the last one, my program just doesn't want to insert the value.
For example, for the array {1, 2, 3, 4, 6} and the value 5, the array should be {1, 2, 3, 4, 5, 6}, but for the value 7 my array is looking like {1, 2, 7, 4, 6, 0}.
#include <stdio.h>
void insert(int val, int *n, int v[])
{
int index;
index = n - 1;
if (n == 0)
{
v[0] = val; // check if array is empty
n = n + 1; // v[0] becomes the given value
} // increase size of array
if (val > v[index])
{
v[index+1] = val; // given value is bigger than the last value in array
n = n + 1; // increase size
}
else
{
while (index >= 0 && v[index] > val)
{
v[index+1] = v[index]; //shift items to the right
index--;
}
v[index + 1] = val; //after moving elements to the right
n = n + 1; // i set the value to the valid position
}
}
void display(int n, int v[])
{
int i;
for (i = 0;i < n; i++)
printf("%d ", v[i]);
}
int main(void)
{
int v[10] = { 12, 23, 34, 41, 69, 71, 81, 91, 100 };
int n;
n = 9; // size of array
insert(101,n,v); // 101 is given value to insert
display(n,v);
return 0;
}
You have a couple of mistakes:
You are passing int instead of int * so you're not able to update array size
You are not correctly placing value in the array
This is how your code should look like:
#include <stdio.h>
void insert(int val, int *nPtr, int v[]);
void display(int n, int v[]);
int main(void) {
int v[10] = {12, 23, 34, 41, 69, 71, 81, 91, 100};
int n;
n = 9;
insert(101, &n, v);
display(n, v);
return 0;
}
void insert(int val, int *nPtr, int v[]) {
int n = *nPtr;
int i, j;
int k = 0;
for (i = 0; i < n + 1; i++)
if (!k) {
if (v[i] > val || i == n) {
for (j = n - 1; j >= i; j--) {
v[j + 1] = v[j];
}
v[i] = val;
n++;
k = 1;
}
}
*nPtr = n;
}
void display(int n, int v[]) {
int i;
for (i = 0; i < n; i++)
printf("%d ", v[i]);
printf("\n");
}
You can also try to insert number on the beginning, for example 0 and it will still work.
So, I have this so far. I'm trying to find the two largest numbers in an array and return them. I looked up a lot of resources online, and most of them say "call by reference" is the way to go. But I've no idea how to make it work with my program. For example, I saw this example online:
void Calculate(int x, int y, int* prod, int* quot)
{
*prod = x*y;
*quot = x/y;
}
int x = 10,y = 2, prod, quot;
Calculate(x, y, &prod, ")
How does the above program actually "return"? How do I print the return values to the console?
#include "stdio.h"
void largest_two( int numbers[], int len, int *largest, int *next_largest){
int i, temp;
*largest = numbers[0];
*next_largest = numbers[1];
if(*largest < *next_largest){
temp = *next_largest;
*largest = *next_largest;
*next_largest = temp;
}
for (i=0; i<sizeof(numbers); i++) {
if(numbers[i]>= *largest){
*largest = numbers[i];
*next_largest = *largest;
}
else if ( numbers[i] > *next_largest){
*next_largest = numbers[i];
}
}
}
int main() {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int len = 3;
int largest, next_largest;
//==>??? printf("%d %d", largest_two(numbers, len, &largest, &next_largest));
}
Sides' from the pointer issues (you should read a tutorial / book on them), your main problem is that you're attempting to print the single return value of a function with return type void which means it won't return at all.
Your code:
int main() {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int len = 10; // sizeof(numbers)
int largest, next_largest;
largest_two(numbers, len, &largest, &next_largest);
printf("%d %d", largest, next_largest);
}
Keep in mind this is still not entirely correct, but it does adress your problem of printing the numbers.
Also, passing len means you shouldn't do this for (i=0; i<sizeof(numbers); i++) but this instead for (i=0; i<len; i++)
Firstly, this line:
for (i=0; i<sizeof(numbers); i++)
is not correct. You want this to be instead:
for (i=0; i<len; i++)
which should be passed to largest_two() as sizeof numbers/sizeof numbers[0], which is the actual length of the array.
I also suggest setting largest and next_largest to INT_MIN from <limits.h>, and then finding these values from their. It seems you are also having trouble with pointers, and it would be best to use them only when needed.
Here is an example which simplifies your approach, which finds the largest and second largest element in one loop of the array. It also only uses pointers when needed.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAYSIZE(x) (sizeof x/sizeof x[0])
void largest_two(int numbers[], size_t len, int *largest, int *next_largest);
int main(void) {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int largest, next_largest;
largest_two(numbers, ARRAYSIZE(numbers), &largest, &next_largest);
printf("largest = %d\nnext_largest = %d\n", largest, next_largest);
}
void largest_two(int numbers[], size_t len, int *largest, int *next_largest) {
int max, smax;
max = smax = INT_MIN;
for (size_t i = 0; i < len; i++) {
if (numbers[i] > max) {
smax = max;
max = numbers[i];
} else if (numbers[i] > smax && numbers[i] < max) {
smax = numbers[i];
}
}
*largest = max;
*next_largest = smax;
}
Output:
largest = 8
next_largest = 6
Second dataset:
int numbers[] = {3, 1, 6, 3, 6, 2, 8, 0, 8, 7};
Output:
largest = 8
next_largest = 7
I am a rookie, and I hope this question is not a naive one!
I have the following function, where I use elements of one array as indices of another. However, despite my making no changes to the former, I see that the elements are being modified. The code is as follows:
void convert_to_bitmap(int n_shapes, int sizex, int sizey,
int ll_x[n_shapes], int ll_y[n_shapes],
int ur_x[n_shapes], int ur_y[n_shapes],
int shapes_ll_bitmap[sizex][sizey],
int shapes_ur_bitmap[sizex][sizey] )
{
int i;
for (i = 0; i < n_shapes; i++)
{
printf("%d, %d, %d, %d check1\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
}
for (i = 0; i < n_shapes; i++)
{
shapes_ll_bitmap[ll_x[i]][ll_y[i]] = 1;
shapes_ur_bitmap[ur_x[i]][ur_y[i]] = 1;
printf("%d, %d, %d, %d check2\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
}
}
And, the output shows that the first array has changed when I do so. Is there some way to keep it immutable?
Output:
0, 0, 0, 7 check1
0, 9, 0, 15 check1
1, 0, 1, 7 check1
1, 9, 1, 15 check1
2, 13, 2, 15 check1
2, 17, 2, 24 check1
2, 26, 2, 32 check1
0, 0, 0, 7 check2
0, 9, 0, 15 check2
1, 0, 1, 7 check2
1, 9, 1, 15 check2
1, 13, 2, 15 check2
2, 1, 2, 1 check2
1, 26, 2, 32 check2
This is how I invoke the function in main():
convert_to_bitmap(n_shapes, sizex, sizey, ll_x, ll_y, ur_x, ur_y, shapes_ll_bitmap, shapes_ur_bitmap);
And the declaration and initialization of the matrices in int main() is as follows:
int ll_x[n_shapes];
int ll_y[n_shapes];
int ur_x[n_shapes];
int ur_y[n_shapes];
int sizex;
int sizey;
int shapes_ll_bitmap[sizex][sizey];
int shapes_ur_bitmap[sizex][sizey];
for (i=0; i < sizex; i++)
{
for (j = 0; j < sizey; j++)
{
shapes_ll_bitmap[i][j] = 0;
shapes_ur_bitmap[i][j] = 0;
}
printf("\n");
}
Thank you!
Edit:
Here's some self-contained code:
int main(void)
{
enum { MAX_SHAPES = 100000 };
struct Rectangle rect_array[MAX_SHAPES];
int n_shapes = read_shapes_rpt("shapes.rpt", MAX_SHAPES, rect_array);
int i, j;
float pitch_x = 0.044;
float pitch_y = 0.042;
float ll_x_flt[n_shapes];
float ll_y_flt[n_shapes];
float ur_x_flt[n_shapes];
float ur_y_flt[n_shapes];
int ll_x[n_shapes];
int ll_y[n_shapes];
int ur_x[n_shapes];
int ur_y[n_shapes];
int sizex;
int sizey;
int shapes_ll_bitmap[sizex][sizey];
int shapes_ur_bitmap[sizex][sizey];
for (i=0; i < sizex; i++)
{
for (j = 0; j < sizey; j++)
{
shapes_ll_bitmap[i][j] = 0;
shapes_ur_bitmap[i][j] = 0;
}
printf("\n");
}
if (n_shapes > 0)
{
transform_to_shape_bit_locations(n_shapes, rect_array, ll_x_flt, ll_y_flt, ur_x_flt, ur_y_flt, ll_x, ll_y, ur_x, ur_y, &pitch_x, &pitch_y, &sizex, &sizey);
convert_to_bitmap(n_shapes, sizex, sizey, ll_x, ll_y, ur_x, ur_y, shapes_ll_bitmap, shapes_ur_bitmap);
printf("%d\n%d\n%d\n", n_shapes, sizex, sizey);
/* Use the shapes that were read */
}
return 0;
}
My shapes.rpt file contained the following csv values:
1.408,529.237,1.43,529.523
1.408,529.597,1.43,529.883
1.452,529.237,1.474,529.523
1.452,529.597,1.474,529.883
1.496,529.777,1.518,529.883
1.496,529.957,1.518,530.243
1.496,530.317,1.518,530.564
Your variables sizex and sizey are left uninitialized. Which means that your matrices shapes_ll_bitmap and shapes_ur_bitmap have unpredictable size (the behavior is actually undefined).
Note that when you actually assign meaningful values to your your sizex and sizey later, it is already too late. The matrices are already declared with indeterminate sizes and that's final. Once the matrices are declared, any changes to the values of sizex and sizey will have no effect on the matrices.
Your matrices end up with some indeterminate size, which results in out-of-bounds access inside convert_to_bitmap function and destruction of unrelated memory values.
How exactly is the binary hash map working here? As in, how does it initialise only binMap[elements in arr] to 1 and the rest to 0?
#include <stdio.h>
#define MAX 100000
void printPairs(int arr[], int arr_size, int sum)
{
int i, temp;
bool binMap[MAX] = {0}; /*initialize hash map as 0*/
for(i = 0; i < arr_size; i++)
{
temp = sum - arr[i];
if(temp >= 0 && binMap[temp] == 1)
{
printf("Pair with given sum %d is (%d, %d) \n", sum, arr[i], temp);
}
binMap[arr[i]] = 1;
}
}
int main()
{
int A[] = {1, 4, 45, 6, 10, 8};
int n = 16;
int arr_size = 6;
printPairs(A, arr_size, n);
getchar();
return 0;
}
As in how does it initialise only binMap[elements in arr] to 1 and the
rest to 0?
With this
bool binMap[MAX] = {0};
The every element in the array binMap array is initialized to 0 (actually it sets only binMap[0] to 0 but due to implicit initialization in C, the rest of the elements are set to 0 too) at first.
Then with this in the loop,
binMap[arr[i]] = 1;
the indexes given by the elements of arr are set to 1. For example, if arr[i] is 45 then binMap[45] is set to 1.