Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with code

mrvendetta1@yahoo.com
Guest
 
Posts: n/a
#1: Jan 24 '06
hi any hints would be great!
#include <stdio.h>
#define MySize 8
void FindPosition(int array[]);
{
int array1[MySize] = {0,0,0,0,0,1,0,1};
FindPosition(array1)
void FindPosition(int array[])
{
int counter;

for(counter = 7; counter >= 0; --counter)
{

if(array[counter] == 1)
{
printf("%d", counter);
}
}


}
the output reads 7 and 5, and obviously if you are counting backward,
it would be 2 and 0.
its supposed to read through them and print the position 2 and 0, what
is wrong with my counter.


Vladimir S. Oka
Guest
 
Posts: n/a
#2: Jan 24 '06

re: problem with code


mrvendetta1@yahoo.com wrote:
[color=blue]
> hi any hints would be great!
> #include <stdio.h>[/color]

Vertical spacing is cheap...
[color=blue]
> #define MySize 8[/color]

Using MYSIZE or MY_SIZE would be more orthodox...
[color=blue]
> void FindPosition(int array[]);[/color]

You're missing:

int main(void)
[color=blue]
> {
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)[/color]

You're missing a closing ";" in the previous line...

You'd want this (below) out of main():
[color=blue]
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7; counter >= 0; --counter)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", counter);
> }
> }
>
>
> }[/color]

You're missing:

return 0;
}
[color=blue]
> the output reads 7 and 5, and obviously if you are counting backward,
> it would be 2 and 0.
> its supposed to read through them and print the position 2 and 0, what
> is wrong with my counter.[/color]

No, obviously, what you're getting is correct, although I fail to see
how the code you posted would have compiled, let alone ran.

It will be instructive if you unfold the loop manually, and see for
yourself why my statement in the previous paragraph is correct, and
yours isn't. Pen(cil) and paper are not obsolete, even in the XXI
century.

Also, try to post a compilable example, or at least syntactically
correct excerpt.

Cheers

Vladimir

PS
There's bound to be more problems in your code, but what I pointed out
should be enough to get you going.

--
Tax reform means "Don't tax you, don't tax me, tax that fellow behind
the tree."
-- Russell Long

mrvendetta1@yahoo.com
Guest
 
Posts: n/a
#3: Jan 24 '06

re: problem with code


hi any hints would be great!
#include <stdio.h>
#define MySize 8
void FindPosition(int array[]);
int main()
{
int array1[MySize] = {0,0,0,0,0,1,0,1};
FindPosition(array1)
return 0;
void FindPosition(int array[])
{
int counter;

for(counter = 7; counter >= 0; --counter)
{

if(array[counter] == 1)
{
printf("%d", counter);
}
}

}
// second draft,sloppy with last onw
the output reads 7 and 5, and obviously if you are counting backward,
it would be 2 and 0.
its supposed to read through them and print the position 2 and 0, what
is wrong with my counter.

mrvendetta1@yahoo.com
Guest
 
Posts: n/a
#4: Jan 24 '06

re: problem with code


#include <stdio.h>
#define MySize 8
void FindPosition(int array[]);
int main()
{
int array1[MySize] = {0,0,0,0,0,1,0,1};

FindPosition(array1);

return 0;
}


void FindPosition(int array[])
{
int counter;

for(counter = 7; counter >= 0; counter--)
{
printf("%d", counter);
if(array[counter] == 1)
{
printf("%d", counter);
}
}


}
compiler fine answer not, anyone?

Martin Ambuhl
Guest
 
Posts: n/a
#5: Jan 24 '06

re: problem with code


mrvendetta1@yahoo.com wrote:[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> {
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7; counter >= 0; --counter)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", counter);
> }
> }
>
>
> }
> the output reads 7 and 5, and obviously if you are counting backward,
> it would be 2 and 0.
> its supposed to read through them and print the position 2 and 0, what
> is wrong with my counter.
>[/color]
#include <stdio.h>

void class(size_t catch, int public[catch])
{
size_t private;

for (private = 0; private < catch; private++)

if (public[private])
printf("%lu ", (unsigned long) (catch - private - 1));
putchar('\n');
}

int main(void)
{
int try[] = { 0, 0, 0, 0, 0, 1, 0, 1 };
size_t const_cast = sizeof try / sizeof *try;
class(const_cast, try);
return 0;
}
Vladimir S. Oka
Guest
 
Posts: n/a
#6: Jan 24 '06

re: problem with code


mrvendetta1@yahoo.com wrote:
[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> int main()
> {
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)[/color]

Still missing a ";" here...
[color=blue]
> return 0;[/color]

and a:

}

here, to close off main().
[color=blue]
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7; counter >= 0; --counter)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", counter);
> }
> }
>
> }
> // second draft,sloppy with last onw
> the output reads 7 and 5, and obviously if you are counting backward,
> it would be 2 and 0.
> its supposed to read through them and print the position 2 and 0, what
> is wrong with my counter.[/color]

All other comments in my other post still stand. Unfold the loop
manually and see why you're wrong.

Cheers

Vladimir

--
Jacquin's Postulate on Democratic Government:
No man's life, liberty, or property are safe while the
legislature is in session.

Vladimir S. Oka
Guest
 
Posts: n/a
#7: Jan 24 '06

re: problem with code


mrvendetta1@yahoo.com wrote:
[color=blue]
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> int main()
> {
> int array1[MySize] = {0,0,0,0,0,1,0,1};
>
> FindPosition(array1);
>
> return 0;
> }
>
>
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7; counter >= 0; counter--)
> {
> printf("%d", counter);
> if(array[counter] == 1)
> {
> printf("%d", counter);
> }
> }
>
>
> }
> compiler fine answer not, anyone?[/color]

The answer of the code above _is_ correct.

Try not counting backwards...

Cheers

Vladimir

PS
Quote when replying to posts (even your own) as many people won't know
what you're on about otherwise.

--
There's a fine line between courage and foolishness. Too bad it's not
a fence.

Guillaume
Guest
 
Posts: n/a
#8: Jan 24 '06

re: problem with code


Martin Ambuhl wrote:[color=blue]
> #include <stdio.h>
>
> void class(size_t catch, int public[catch])
> {
> size_t private;
>
> for (private = 0; private < catch; private++)
>
> if (public[private])
> printf("%lu ", (unsigned long) (catch - private - 1));
> putchar('\n');
> }
>
> int main(void)
> {
> int try[] = { 0, 0, 0, 0, 0, 1, 0, 1 };
> size_t const_cast = sizeof try / sizeof *try;
> class(const_cast, try);
> return 0;
> }[/color]

It works alright.
But the name of the identifiers?
Hopefully it's a nasty joke. :D
Lew Pitcher
Guest
 
Posts: n/a
#9: Jan 24 '06

re: problem with code


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mrvendetta1@yahoo.com wrote:[color=blue]
> hi any hints would be great![/color]
[snip][color=blue]
> the output reads 7 and 5, and obviously if you are counting backward,
> it would be 2 and 0.[/color]

???
[color=blue]
> its supposed to read through them and print the position 2 and 0, what
> is wrong with my counter.[/color]

Think about it. Here's some hints

In C, arrays start with element 0, and each successive element is
indexed with an index 1 greater than the one before.

An 8 element array, thus, would consist of 8 elements, indexed as [0],
[1], [2], ... [6], [7]

To access any element, you use the corresponding element index, so to
access the 8'th element, you would use [7]

You are accessing elements of your array. Thus, you must use the element
index to access each individual element.

Your 1st element ([0]) contains 0, your 6'th element ([5]) contains 1,
and your 8th element ([7]) contains 1




- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFD1ngdagVFX4UWr64RAgABAKCcxwFzKN6w+qmFozsdl4 cjG7bLsQCfbr2S
Y+2Ej76tmkNI31eLuMid+40=
=Qs6l
-----END PGP SIGNATURE-----
Mark B
Guest
 
Posts: n/a
#10: Jan 24 '06

re: problem with code



"Guillaume" <"grsNOSPAM at NOTTHATmail dot com"> wrote in message
news:43d6786e$0$26396$7a628cd7@news.club-internet.fr...[color=blue]
> Martin Ambuhl wrote:[color=green]
>> #include <stdio.h>
>>
>> void class(size_t catch, int public[catch])
>> {
>> size_t private;
>>
>> for (private = 0; private < catch; private++)
>>
>> if (public[private])
>> printf("%lu ", (unsigned long) (catch - private - 1));
>> putchar('\n');
>> }
>>
>> int main(void)
>> {
>> int try[] = { 0, 0, 0, 0, 0, 1, 0, 1 };
>> size_t const_cast = sizeof try / sizeof *try;
>> class(const_cast, try);
>> return 0;
>> }[/color]
>
> It works alright.
> But the name of the identifiers?[/color]
Brilliant! And perfectly legal in C.
[color=blue]
> Hopefully it's a nasty joke. :D[/color]
No, hopefully OP sees this and turns it in exactly as written...
I'm sure he'll get an A+ if he does ;-)


Lew Pitcher
Guest
 
Posts: n/a
#11: Jan 24 '06

re: problem with code


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Guillaume wrote:[color=blue]
> Martin Ambuhl wrote:
>[color=green]
>> #include <stdio.h>
>>
>> void class(size_t catch, int public[catch])
>> {
>> size_t private;
>>
>> for (private = 0; private < catch; private++)
>>
>> if (public[private])
>> printf("%lu ", (unsigned long) (catch - private - 1));
>> putchar('\n');
>> }
>>
>> int main(void)
>> {
>> int try[] = { 0, 0, 0, 0, 0, 1, 0, 1 };
>> size_t const_cast = sizeof try / sizeof *try;
>> class(const_cast, try);
>> return 0;
>> }[/color]
>
>
> It works alright.
> But the name of the identifiers?
> Hopefully it's a nasty joke. :D[/color]

If I understand Martin correctly, it is his way of stating
"C is not a subset of C++"
by showing that certain C++ keywords are valid C identifiers, and use of
such will cause a C++ compiler to reject the source code out of hand.

P'haps he is trying to ensure that the OP is not looking for a C++
answer to a C question. :-)


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFD1n6pagVFX4UWr64RAt+iAJ9yg/QRA+pbTq12g0SaSLWN5tmjAwCfen1r
Sh4uaEgKBLPKg4atPSH12mI=
=9gWH
-----END PGP SIGNATURE-----
Guillaume
Guest
 
Posts: n/a
#12: Jan 24 '06

re: problem with code


Mark B wrote:[color=blue]
> Brilliant! And perfectly legal in C.[/color]

Indeed, it was a good one.
[color=blue][color=green]
>> Hopefully it's a nasty joke. :D[/color]
> No, hopefully OP sees this and turns it in exactly as written...
> I'm sure he'll get an A+ if he does ;-)[/color]

Especially since his problem was not even related to a C problem.
Keith Thompson
Guest
 
Posts: n/a
#13: Jan 24 '06

re: problem with code


mrvendetta1@yahoo.com writes:[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> {
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7; counter >= 0; --counter)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", counter);
> }
> }
>
>
> }
> the output reads 7 and 5, and obviously if you are counting backward,
> it would be 2 and 0.
> its supposed to read through them and print the position 2 and 0, what
> is wrong with my counter.[/color]

Nothing is wrong with it; it's doing exactly what it's supposed to do.
There's something wrong with your expectations if you expect it to
print 2 and 0.

Please post the actual code that you compiled, not some approximation
of it; we can't guess what errors you might have introduced by
re-typing it. You did so later in the thread, but I wanted to
emphasize the point.

The array has elements equal to 1 at indices 5 and 7. If you search
the array for elements equal to 1, you'll find them at indices 5 and 7
(or 7 and 5) regardless of the order in which you search.

If you want to display something other than the indices at which those
elements appear, you'll need to do some additional computation.

You say you want the program to print 2 and 0 in this specific case.
Try defining the problem for the general case. If you can create a
precise problem description, in English, of just what you want the
program to do, you'll be half way to solving the problem, or at least
determining where your program doesn't meet its requirements.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mark McIntyre
Guest
 
Posts: n/a
#14: Jan 24 '06

re: problem with code


On 24 Jan 2006 10:44:28 -0800, in comp.lang.c , mrvendetta1@yahoo.com
wrote:

(the same stuff again, fixing up some mistakes, but not retaining any
context)

You ignored this bit of the previous poster's advice:

"It will be instructive if you unfold the loop manually, and see for
yourself why my statement in the previous paragraph is correct, and
yours isn't. Pen(cil) and paper are not obsolete, even in the XXI
century."

If you use this method, you will soon see why your programme behaves
as it does.

Also, please read the below.
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header. For more information, please go here
<http://cfaj.freeshell.org/google/>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Herbert Rosenau
Guest
 
Posts: n/a
#15: Jan 25 '06

re: problem with code


On Tue, 24 Jan 2006 18:25:25 UTC, mrvendetta1@yahoo.com wrote:
[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> {
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7; counter >= 0; --counter)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", counter);
> }
> }
>
>
> }
> the output reads 7 and 5, and obviously if you are counting backward,
> it would be 2 and 0.
> its supposed to read through them and print the position 2 and 0, what
> is wrong with my counter.
>[/color]
Nothing. It works as aspected.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
bluesky
Guest
 
Posts: n/a
#16: Jan 25 '06

re: problem with code



mrvendetta1@yahoo.com 写道:
[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> {[/color]
int j;[color=blue]
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7,j=0; counter >= 0; --counter,j++)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", j);
> }
> }
>
>
> }[/color]
you can try this change,the variable j will give you the counter you
want.the "counter"that in your programme is the NO. of the array's
element,that not the exactly counter,because it doesn't count the
time of your loop.

bluesky
Guest
 
Posts: n/a
#17: Jan 25 '06

re: problem with code



mrvendetta1@yahoo.com 写道:
[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> {[/color]
int j;[color=blue]
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7,j=0; counter >= 0; --counter,j++)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", j);
> }
> }
>
>
> }[/color]
you can try this change,the variable j will give you the counter you
want.the "counter"that in your programme is the NO. of the array's
element,that not the exactly counter,because it doesn't count the
time of your loop.

bluesky
Guest
 
Posts: n/a
#18: Jan 25 '06

re: problem with code



mrvendetta1@yahoo.com 写道:
[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> {[/color]
int j;[color=blue]
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7,j=0; counter >= 0; --counter,j++)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", j);
> }
> }
>
>
> }[/color]
you can try this change,the variable j will give you the counter you
want.the "counter"that in your programme is the NO. of the array's
element,that not the exactly counter,because it doesn't count the
time of your loop.

August Karlstrom
Guest
 
Posts: n/a
#19: Jan 25 '06

re: problem with code


mrvendetta1@yahoo.com wrote:[color=blue]
> hi any hints would be great!
> #include <stdio.h>
> #define MySize 8
> void FindPosition(int array[]);
> {
> int array1[MySize] = {0,0,0,0,0,1,0,1};
> FindPosition(array1)
> void FindPosition(int array[])
> {
> int counter;
>
> for(counter = 7; counter >= 0; --counter)
> {
>
> if(array[counter] == 1)
> {
> printf("%d", counter);
> }
> }
>
>
> }
> the output reads 7 and 5, and obviously if you are counting backward,
> it would be 2 and 0.
> its supposed to read through them and print the position 2 and 0, what
> is wrong with my counter.[/color]

What's wrong? Well, what you posted is not a valid C program, not even a
valid part of a C program. Come back when you have something that
compiles or have questions about why it doesn't. (Hint: Use copy and
paste between your text editor and your mail program.)

Recommended reading:

http://www.catb.org/~esr/faqs/smart-questions.html


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Closed Thread