473,465 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with code

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.

Jan 24 '06 #1
18 1636
mr*********@yahoo.com wrote:
hi any hints would be great!
#include <stdio.h>
Vertical spacing is cheap...
#define MySize 8
Using MYSIZE or MY_SIZE would be more orthodox...
void FindPosition(int array[]);
You're missing:

int main(void)
{
int array1[MySize] = {0,0,0,0,0,1,0,1};
FindPosition(array1)
You're missing a closing ";" in the previous line...

You'd want this (below) out of main():
void FindPosition(int array[])
{
int counter;

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

if(array[counter] == 1)
{
printf("%d", counter);
}
}
}
You're missing:

return 0;
}
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.


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

Jan 24 '06 #2
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.

Jan 24 '06 #3
#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?

Jan 24 '06 #4
mr*********@yahoo.com wrote:
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.

#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;
}
Jan 24 '06 #5
mr*********@yahoo.com wrote:
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)
Still missing a ";" here...
return 0;
and a:

}

here, to close off main().
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.


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.

Jan 24 '06 #6
mr*********@yahoo.com wrote:
#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?


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.

Jan 24 '06 #7
Martin Ambuhl wrote:
#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;
}


It works alright.
But the name of the identifiers?
Hopefully it's a nasty joke. :D
Jan 24 '06 #8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mr*********@yahoo.com wrote:
hi any hints would be great! [snip] 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.


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-----
Jan 24 '06 #9

"Guillaume" <"grsNOSPAM at NOTTHATmail dot com"> wrote in message
news:43***********************@news.club-internet.fr...
Martin Ambuhl wrote:
#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;
}
It works alright.
But the name of the identifiers?

Brilliant! And perfectly legal in C.
Hopefully it's a nasty joke. :D

No, hopefully OP sees this and turns it in exactly as written...
I'm sure he'll get an A+ if he does ;-)
Jan 24 '06 #10
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Guillaume wrote:
Martin Ambuhl wrote:
#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;
}

It works alright.
But the name of the identifiers?
Hopefully it's a nasty joke. :D


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-----
Jan 24 '06 #11
Mark B wrote:
Brilliant! And perfectly legal in C.


Indeed, it was a good one.
Hopefully it's a nasty joke. :D

No, hopefully OP sees this and turns it in exactly as written...
I'm sure he'll get an A+ if he does ;-)


Especially since his problem was not even related to a C problem.
Jan 24 '06 #12
mr*********@yahoo.com writes:
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.


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) ks***@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.
Jan 24 '06 #13
On 24 Jan 2006 10:44:28 -0800, in comp.lang.c , mr*********@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 =----
Jan 24 '06 #14
On Tue, 24 Jan 2006 18:25:25 UTC, mr*********@yahoo.com wrote:
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.

Nothing. It works as aspected.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Jan 25 '06 #15

mr*********@yahoo.com 写道:
hi any hints would be great!
#include <stdio.h>
#define MySize 8
void FindPosition(int array[]);
{ int j; 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);
}
}
}

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.

Jan 25 '06 #16

mr*********@yahoo.com 写道:
hi any hints would be great!
#include <stdio.h>
#define MySize 8
void FindPosition(int array[]);
{ int j; 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);
}
}
}

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.

Jan 25 '06 #17

mr*********@yahoo.com 写道:
hi any hints would be great!
#include <stdio.h>
#define MySize 8
void FindPosition(int array[]);
{ int j; 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);
}
}
}

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.

Jan 25 '06 #18
mr*********@yahoo.com wrote:
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.


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.
Jan 25 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two...
6
by: harry | last post by:
Hi, I have a program that runs on multiple client pc's. Occasionally one or more of those pc's use VPN to connect to another corporate network. When using VPN they need to set proxy server in...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.