Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

I suck, plz help lol...


  • Please log in to reply

#31
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Extremely sorry, I misread Post#26 :) :tazz:
I thought your teacher wanted to always comapre the first and second elements and shift the array.

Here is the new approach(logic).
Make an array a
Take a variable named max_num and initalize it to the first element in the array.
Keep a index i for every recursion.
Compare if:
max_num < a[i]
If yes, make new max_num as a
Increment i and do a recursion.
For the exit condition, compare i with n (the no. elements in the array)


As for your code:
You can't declare an array, with size n, like that
There are two ways it can be accomplished:
1) Declare a huge array of size 100, but only use n elements as given by the user.
2)It can also be done by dynamic allocation using [i]malloc
fuction. If you teacher has not thought you about dynamic allocation, I suggest you use the first alternative.

Edited by darth_ash, 04 October 2005 - 05:20 AM.

  • 0

Advertisements


#32
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Okay, i did the program and i'm pretty sure it'll run, but it won't compile cause it has "too many arguments" in the decleration of the function "max"... i don't know what the problem is :

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int max(int *arr, int max_num, int i, int n);
void main(){
int max_num,x,arr[100],i=0,n;
clrscr();
printf("Enter size of array (not larger than 100):");
scanf("%d", &n);
max_num=arr[0];
x=max(arr,max_num,i,n);
printf("The maximum number in the array is %d", x);
getch();
}
int max(int *arr, int max_num, int i, int n)
{
if (i<n){
if (max_num<arr[i]){
max_num=arr[i];
i++;
return max(arr,max_num,i,n);
}
else {
i++;
return max(arr,max_num,i,n);
}
}
return max_num;
}
  • 0

#33
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Thats wierd, it compiled successfully on my PC; which version of TurboC compiler are you using?
I thought you had a pre-defined array, which you had to define in the program, why did'nt you define it. If its a predefined array we don't need n.
  • 0

#34
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
I meant that in the program i have an array, and then the user can determine the size, the way we did it is ok :tazz:... no worries. tnx man, i'll send it to my teacher.
  • 0

#35
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Hey, i'm back with a new problem lol..
I need to do a program that prints out a list of all the binary numbers that are n long, meaning if n=3, then i need to print 100,101,110,111..

in the class i didn't really understand, but i know it's somthing to do with printing out a 1 or a 0 in turn, actually, i don't really have a clue as to how to do this, a little help maybe? :tazz:
  • 0

#36
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Binary numbers have only 2 symbols viz. 0 and 1, unlike the decimal system used in eveyday life, which has symbols from 0 to 9.

Lets take the decimal system for instance, all numbers in the decimal system are made up from the symbols from 0 to 9. Note it is called decimal system because we have 10 basic symbols(0,1,2,3,4,5,6,7,8,9) which make the full number system.
It starts with all the symbols individually i.e. 0 to 9, when we reach 9, we go to next level, i.e. 10. We go to the next level because we have reached the last number on the current level.

The same should be applied to the binary number system, but instead of 10 basic symbols we have only 2, viz. 0 and 1. As we did in the decimal system, we start of with the basic symbols are the number and then go to the next level, if we reach the last number of a level then again we go to the next level; below is the binary number system:
0
1
10 (Since 1 is the last number at the curret level, we goto the next level)
11
100 (Again since 11 was the last number at the current level, we goto the next level)
101
....
....


Sadly there is no binary data-type in C. So we will have to use the integer data-type which uses the decimal system. So we will need to convert decimal-number to binary logically.
The binary number equivalents for all decimal numbers can be found by, repeatively dividing numbers by 2 and then taking the remainders in reverse order.
(Note: Keep this method in mind, because we will use it for the approach.)
Let me illustrate with an example:
Consider decimal number 49, lets convert it to binary.
49/2 = 24 r 1
24/2 = 12 r 0
12/2 = 6 r 0
6/2 = 3 r 0
3/2 = 1 r 1
1/2 = 0 r 1
Now take the remainders in reverse order, this will give 49 in binary, i.e.:
110001.

If the above concepts are clear, then only I'll go-on to the approach(logic).
  • 0

#37
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Of course it's clear, i learned it last year when we started C. go on please.
  • 0

#38
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Well then you know that binary nos. go to the next level when they reach a no. (n)*2 and the first no. of each level is (n-1)*2 (where n is current level no.), e.g.:
At level 1(n=1), the first no. is 0 and last no. is 1
At level 2(n=2), the first no. 2(binary 10) and the last no. is 3 (binary 11)
At level 3(n=3), the first no. is 4(binary 100) and the last no. is 7(binary 111).

So for a particular n(which is alos user input) the nos. range from (n-1)*2 to n*2, now we have to use the binary to decimal conversion method to convert and display the nos. within the above range.

This is just the basic outline of the approach you can follow, do you want me to explain in terms of loops and ifs like I have done previously or this enough?
  • 0

#39
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Hmmm.. i think i understand, but i tried to think about if user input is different, then how do i set the start num and the last num for the binary nums i display, meaning if once n=3 and another time n=5, how do i set the program to automatically go once from 100-111 and another time from 10000-11111?

(i need to do this recursivly btw...)

Edited by yafim, 07 October 2005 - 03:29 PM.

  • 0

#40
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
You can keep a counter, i that starts from (n-1)*2 and goes till n*2.
And for every value of i find the binary eqivalent.
  • 0

Advertisements


#41
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
ugh.. i still don't get it... if the usr puts in n=5, how is the program going to print out 10000?
  • 0

#42
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Extremely sorry again, :) :tazz:
In the previos posts I meant that i should start from 2^(n-1) (i.e. 2 to the power of (n-1)) and goto (2^n)-1 (i.e. 2power n and then subtract by 1).

So if n=5, i ranges from 16(i.e. 2^(n-1)) to 31(to (2^n)-1).
Now all you have to do is, convert the values of i to binary in the range.

Edited by darth_ash, 08 October 2005 - 03:34 AM.

  • 0

#43
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Um.. okay, i don't think i'v ever converted numbers to binary in a program, how do i do it?

(and when n=5 isn't it from 16 to 24? not 31...)
  • 0

#44
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
n=5 ranges from 16(2^(n-1)) to 31((2^n)-1).
31 when converted to binary gives 11111, which is the last binary number at level-5.

To convert try to use the division method I used in Post#36.
  • 0

#45
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Okay, i'll try to think more on this and how to excute it, it's pretty hard to understand how to do this recursivly and to convert the numbers. I'll send a msg about how i'm doing, although i can't do it tommorow i think cause i'm very busy, i'll try.

oooh.. lol, i thought of (2^n)-1 as 2^5=25, meaning 5 to the 2nd power.. i just got it that it's 2 to the 5th... sry about that. (becasue 2^4 and 4^2 both equal 16 so i got confused.)

Edited by yafim, 08 October 2005 - 03:22 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP