Instance of variable [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a c code
int foo( int *p, int n){
int a[81];
int i,v;
for( i = 0; i < n ; i++){
a[i]=p[i]
if( n == 1 ) return a[0];
v=foo ( a, n-1);
if( v > a[n-1] )
return v;
else
return a[n-1];
}
int main ( void ){
int b[81], i;
for( i = 0; i < 81; i++){
b[i]=rand();
foo(b,81);
return 0;
}
And i need to find out how many instances of variable a will exist( max number) my answer was 81 , but it was wrong and i cant find out what number it should be. How could i determine it?

The main will call the function 82 times and each time the func will call recursively itself 80 times in a loop of decreasing n items.
So altogether it will be 81*81.
EDIT: I didn't notice the return after first iteration so actually it's pretty small number.

Set the number of elements in a to be arbitrarily high.
Build a function to assign elements to a, and only use that function to set elements of a. You could use this function to make sure that you don't attempt to write outside the bounds of a. (Else the behaviour of your program will be undefined and so any results will be meaningless).
Encode a "watermark" static int in that function to record the largest index accessed.
Run your program and note the value of the variable set up for (3).

Declare a global counter - it will be initialized to zero. In the function, at the same scope as 'a' is declared, increment the counter.
Printf() it out at the end of main().

Related

Find Three Identical Numbers in an Array in C with Nested Loops [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to create an application in C that finds three similar elements in an array. For example, 1 2 3 4 3 5 3 and three is repeated three times using nested for loops.
You need to be more clear about your question...
This code will help you if you want to find only the first triple.
And I will refer to the above writers, if this is your HW, its only hurts you.
int findrpt(int* arr,int len){
int cnt = 0;
int i = 0, j = 0;
for(i = 0; i < len; i++)
{
cnt = 0;
for(j = i; j < len - i; j++)
if(arr[j] == arr[i])
cnt++;
if(cnt >= 3)
return arr[i];
}
}
Why use nested loops? its basically O(n^3) time. Basically just sort the array in time O(nlogn) and find the element using single for loop. Benefit of sorting. In case you want to find all triples only then a single loop is required but if you want to check if some specific element is occurs three times, just apply binary search.
If the input elements are restricted to a smaller range like [1,100], you can use a counter array of length 1000 initialized as counter[[i]] = 0 . Now just run a single loop and increment the counter as:
for(i = 0 to length of input_array - 1)
{
counter[input_array[i]]++;
}
if(counter[i] == 3)
input_array[i] is a triplet
In this case you wont even require sorting. Cause the array counter will have the count of each element.
If you want to go nested loops, you can make three nested loops, initialized from 0 to 2 elements of the array and check the condition.
There are other easy ways too.
sort the array and check the condition
Count elements

Why variable b don't change in the for-loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Hy, I want to understand why variable b don't change and when I print it always be as I defined (zero). What's wrong with my code??
With this code I want to find the max integer number (b) that is 10^b <= n (with n given and integer as well).
This is my code (It's written in c):
#include <stdio.h>
#include <math.h>
int lg (int n);
int main() {
int a = 0;
scanf("%d", &a);
printf ("\nN=%d e M=%d\n", a, lg(a));
return 0;
}
int lg (int n) {
double b = 0.0;
for (int i=0; i==n; i++) {
if (pow(10, i) <= n) {
double b = i;
} else {
}
}
return (int)b;
}
You're declaring a new variable named b inside your loop and setting it equal to i. Since that's a different scope, it's not the same variable as the b declared before the loop. Change the line
double b = i;
to
b = i;
Your loop condition is also set to i==n;. That means your loop will only run once if your input is 0. That should probably be i < n;.
There are two variables named b. One whose scope is the function lg, and one whose scope is restricted to the if clause of the for loop. You modify the latter, but return the former. Using the keyword 'double' declares a new varaible. This is a common source of error, and simply turning up the warnings on your compiler should alert you to the problem.
You are declaring "b" variable twice.The "b" variable initialized with 0.0 have scope within the function "lg". And the "b" declared inside if codition have limited scope to that condition only.
Also check the second parameter of for loop i.e "i==n". This is statement would be always true i.e "1" if the value of input ("n") is not zero.
Error 1: i==n: if you compare i==n in a for loop, that loop will only run a time.You must fix it into i<n
Error 2: You repeated a double type. You mustn't declare a variable b in a for loop because the variable b in that loop will disappear from the loop when it stop repeating.Thus, the value of the variable b in the for loop won't assign.
You must delete the double keyword in the for loop, the variable b will change.

Memorization of a recursive algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Question: Given an amount m and an array of numbers b[] print "Yes" if m can be formed by addition of any number of elements from the array without repeating them.
This is what I've come up with; how can I add a memoization matrix into it?
#include<stdio.h>
int check(int b[],int n,int m){
if(m==0){
return 1;
}
if(m<0){
return 0;
}
if(n<=0 && m>0){
return 0;
}
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d",&n);
scanf("%d",&m);
int b[n];
for(int i=0;i<n;i++){
scanf("%d",&b[i]);
}
if(check(b,n,m)>0){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
Can any one help me put a memorization matrix into it ?
The key to the memoization cache consists of the first n elements of b, plus m. Form a string from these, and use it as a key to a hash table.
...But what's the point? With n decreasing in each recursion, there are only two situations where memoization will help:
When a non-recursive call check is made for exactly the same list of numbers and m as an earlier call to check.
When m-b[n-1] == m, which is to say when b[n-1] == 0.
The first situation is unlikely (and decreasingly so as the size of the list increases, which is when memoization would help the most), and the second is easy to avoid. Just replace
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
with
return b[n-1] == 0
? check(b,n-1,m) * 2
: check(b,n-1,m) + check(b,n-1,m-b[n-1]);

Variable Number of for loops with code in between [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So the problem I have is that I want to implement N for loops in the following way (where N is a variable):
for i0=0:MAX
cOR[0] = initial + move[i0];
for i1=0:MAX
cOR[1] = cOR[0] + move[i1];
....
some other stuff inside the final loop
(cOR is a vector of length equal to the number of for loops)
So I found this solution that works when you just have the nested loops (https://stackoverflow.com/a/20577981/3932908) but have been struggling to modify it for my particular case which requires code in between the for loops. Is there a simple way to implement this or is a different approach needed?
The general approach is to
Write a recursive function.
If recursion is not for some reason appropriate for your code (e.g. very long recursion depth, or the ability to suspend the execution is required), then convert the recursive version to an iterative one by explicitly modeling the stack.
Doing №1 is easy:
void f(int depth, int initial, int *cOR)
{
if(your termination condition)
{
// some other stuff inside the final loop, and...
return;
}
for(int i = 0; i < MAX; ++i)
{
cOR[depth] = initial + move[i];
f(depth+1, cOR[depth]);
}
}
And call it like so:
f(0, initial, cOR);
Now we head to №2, i.e. converting to a non-recursive version. The extra state we need is what was stored on the stack before: the values of the i variables. So here we go:
int i[max_depth];
int depth = 0;
for(;;)
{
if(your termination condition)
{
// some other stuff inside the final loop, and...
do {
if(--depth < 0)
return;
} while(++i[depth] >= MAX);
}
else
i[depth] = 0;
cOR[depth] = (depth > 0 ? cOR[depth-1] : initial) + move[i[depth]];
++depth;
}
If you can't estimate max_depth a priori then you can switch to a dynamically allocated array that grows as you need.

Why array self assign value (C program)? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I declared a two dimensional array as row and col and I left the value empty, but when I check the value in each array position, some of the indexes contain some weird number.
You are looking at uninitilized memory. It can have whatever which value to it. You should never trust the value of a variable you haven't initialized.
Access of an uninitialized value is undefined behavior. You can solve your problem quite simply by initializing the array to all zero to begin with, e.g.
int seatNo[5][5] = {{0}};
Now any subsequent access to any of the elements of seatNo will succeed because each element has been initialized to zero. As a rule, especially when you are learning C, you will save yourself grief if you simply initialize ALL your variables.
(you will also want to turn warnings on, e.g. -Wall -Wextra, at minimum, so your compiler will warn you when a variable may be uninitialized)
#include <stdio.h>
int main (void) {
int seatNo[5][5] = {{0}};
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
printf (" seatNo[%d][%d] = %d\n", i, j, seatNo[i][j]);
return 0;
}
Example
$ ./bin/initarray
seatNo[0][0] = 0
seatNo[0][1] = 0
seatNo[0][2] = 0
seatNo[0][3] = 0
seatNo[0][4] = 0
seatNo[1][0] = 0
seatNo[1][1] = 0
<snip>

Resources