473,791 Members | 2,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm by eratosthenos

It is nothing short of embarrassing to feel the need to ask for help on
this. I can't see how I would make the main control for this. What I want
is a for loop and a test condition. And while I know, from things I
pondered 2 decades ago, that a fella can write code without a goto, I'm
stuck.

/* sieve1.c */

#define whatever 20
#define N whatever
#include <stdio.h>

int main(void)
{
int i, A[N+1], m, sum;

/* initialize to 0 */

for (i = 0; i <= N; ++ i) A[i] = 0;

/* output */
printf("Primes less than N are:\n");
for (i = 2; i <= N; ++ i)
{
if (A[i] == 0)
printf("%d

Apr 27 '06
22 2203
CBFalconer wrote:
Bill Pursell wrote:

... snip ...
...
getopt(argc, argv, optstring)
...
and was rejected because the 2nd parameter to getopt is prototyped
as char *const argv[]. So I had to change the declaration of
parse_args to match, and that got me thinking that the argument
list for main might as well match.


Which is just plain wrong. You can modify **argv. You can't
modify *argv. You can't lengthen the string at *argv either.


What is wrong? The prototype for getopt, or prototyping main as taking
(char *const*argv)? It seems that you 2nd and 3rd sentence, are
semantically equivalent to prototyping main as (int argc, char
*const*argv), so I'm not sure what you're saying. Obviously, if we
protoype main as taking a char **, then we CAN change *argv (and I
think you are claiming that we shouldn't):

#include <stdio.h>

int main (int argc, char **argv)

{
printf("argv[0] = %s\n", argv[0]);
*argv += 1;
printf("argv[0] = %s\n", argv[0]);

return 0;
}

Apr 29 '06 #21
Bill Pursell schrieb:
CBFalconer wrote:
Bill Pursell wrote:

... snip ...
...
getopt(argc, argv, optstring)
...
and was rejected because the 2nd parameter to getopt is prototyped
as char *const argv[]. So I had to change the declaration of
parse_args to match, and that got me thinking that the argument
list for main might as well match.


Which is just plain wrong. You can modify **argv. You can't
modify *argv. You can't lengthen the string at *argv either.

What is wrong? The prototype for getopt, or prototyping main as taking
(char *const*argv)? It seems that you 2nd and 3rd sentence, are
semantically equivalent to prototyping main as (int argc, char
*const*argv), so I'm not sure what you're saying. Obviously, if we
protoype main as taking a char **, then we CAN change *argv (and I
think you are claiming that we shouldn't):

#include <stdio.h>

int main (int argc, char **argv)

{
printf("argv[0] = %s\n", argv[0]);
*argv += 1;
printf("argv[0] = %s\n", argv[0]);

return 0;
}

From C99 5.2.2.1:
"The parameters argc and argv and the strings pointed to by the argv
array shall be modifiable by the program, and retain their last-stored
values between program startup and program termination."

This especially does not mention *argv.
I.e. you are allowed to do argv++ and argv[0][0] = 42 but I'd not rely
on argv[0]++ to work. I guess that we do not have "char * const *"
type for the second type only for historical reasons.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 29 '06 #22
>>> >> I know this post is a mess and to read it annoys even myself. My
>> debugger
>> doesn't want to tell me anything, and while the above compiles, it
>> doesn't
>> behave. Furthermore, I'm rusty with polite snipping, so I'll beg
>> your
>> pardon. Joe
[snips everyone but himself]
/* sieve3.c */

#define whatever 20
#define N whatever
#include <stdio.h>

int main(void)
{
int i, A[2*N + 1], index, sum;


I endeavor to take keystrokes and have the prog create the memory at
runtime.
My first problem is that I'm unaware of how to get an integer without having
to build them from chars and then first principles. I can recall no such
K&R
exercise. Let's assume, however that we have an N from the keyboard that
happens to equal 20.

p = malloc( (N + 1) * sizeof(int));
is what I want to write.

Well, it doesn't look like this sieve is going to work tonight nearly as
well
as that 4 by 6 region behind the Redwing's goalie did. One thing that
would help me to know what this code says is having a debugger that is aware
of any executable. Any help is appreciated. Joe
May 2 '06 #23

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

Similar topics

6
8891
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit time to complete. Suppose further that each task has a deadline by which it is expected to finish. IF a task is not finished by the deadline, a standard penalty of $10 is applied. The problem is to find a schedule of the tasks that minimizes the...
16
2665
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects with the same A or B value. But there can be more than one object with A = null or B = null in the bucket. Sometimes there is only one valid solution, sometimes there are more valid solutions, and sometimes there isn't a complete solution at...
10
4985
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement, knowledge of fast and practical algorithms is not commonplace." Hume and Sunday, "Fast String Searching", Software - Practice and Experience, Vol. 21 # 11, pp 1221-48
32
76458
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number */ int is_prime_number(int n)
113
12358
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same algorithm work with strings that may or may not be unicode 3) Number of bytes back must either be <= number of _TCHARs in * sizeof(_TCHAR), or the relation between output size and input size can be calculated simply. Has to take into account the...
4
4287
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
2
7298
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback as possible from users, so we encourage you to test the algorithm and send us your opinion. We would also like to receive enhancements and new versions of the algorithm, developed in other source languages and platforms. Our idea on developing...
0
2973
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm so interest about this encoding algorithms. because our group is going to do a project under the ITU-T Recommendation X.891. this is named as fast infoset. this is an open source project in c language. there is an implementation in java which by...
1
3113
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an important decision making mechanism. Its a "if, else if, else if" structure like:
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.