Prime Number List Program - loops

first of all i want to prove that this is not a homework http://computer.atlas4e.com/
i really need to understand this with an explanation, because I'm trying to understand the basic of the computer by my self thanks for the help
`Prime Number List Program
Write a main program that generates a list of prime numbers. The program should use the PrimeTest subroutine to test whether each of the numbers 2, 3, 4, 5, 6…, 255 & 256 is a prime. Each number that is found to be a prime should be added to a list in memory. The list of primes should be stored in consecutive memory locations. Use a location named StartPrimeList to point to the start address of the prime number list. Use a pointer called PrimeListPtr to point to the (current) end of the list, PrimeListPtr will be incremented every time a prime number is appended to the list.
Use a location named PrN to store the number that you are checking. Start by storing a 2 in PrN. In a loop, you should call PrimeTest to determine whether PrN is a prime. If PrN is a prime, then append it to the list. Then add 1 to PrN and jump back to the start of the loop to test the next value of PrN.`
The output of the program should appear starting at location StartPrimeList as follows:-
Address Contents
[StartPrimeList] 2
[StartPrimeList+1] 3
[StartPrimeList+2] 5
[StartPrimeList+3] 7
…
…
[StartPrimeList+?] Largest Prime <= 256
(251 - 54th prime number starting from 2 <=256)
(257 - 55th prime number >256)
`
Mark Allocations for Program 2
• A program header in the program listing explaining how the program works.
• Sensible/Relevant program comments on sections of assembler language.
• Relevant labelling of loops and data items. [4]
• Program runs successfully on xComputer and produces correct output for given input.
`

"Write a main program that generates a list of prime numbers." This is your basic question. further details are given later to help you answer it.
"The program should use the PrimeTest subroutine to test whether each of the numbers 2, 3, 4, 5, 6…, 255 & 256 is a prime." Can you do this part? If not, then write the best code you can to do it and ask here again with a specific question.
"Each number that is found to be a prime should be added to a list in memory." Can you do this part? If not, then write the best code you can to do it and ask here again with a specific question.
"The list of primes should be stored in consecutive memory locations." Can you do this part? If not, then write the best code you can to do it and ask here again with a specific question.
"Use a location named StartPrimeList to point to the start address of the prime number list." You should have got the message by now. Use the excellent advice given in the question itself to help you. Write your code. If it works, then fine. If it doesn't then post it here, explaining what it should do and what you think the problem might be. Tell us what you have tried, and what didn't fix the problem. That will help us having to repeat your work.
We won't write your program for you. You have to write it; we will help you write it.

Related

Is printing a char* faster than printing a char one at a time?

I have to write a simple program in C that prints to the standard output triangle with two equal edges for given number n. Meaning that for n=3 the output would be:
x
xx
xxx
Now I'm supposed to do two version of this program:
1. Memory conservative.
2. Time conservative.
Now I'm not entirely sure, but I think that the first version would just print x one at a time, and the second would expand the char table one at a time and then print it.
But is printing a char* faster than printing multiple single chars?
You may not be able to observe but building the entire string in memory and then printing it at once is definitely faster in theory. Reason being you will be making less calls to printf function. Each time you call a function there are multiple things that happen in the background like pushing all the current method variables and current location to stack and popping them back after returning.
However as I mentioned you may not be able to observe this difference for smaller inputs because the time needed for each of these operations are small unless you use a computer from 1960s.

How to use scanf without determined amount of input

I've been searching a while for this, and couldn't find an answer. I will be appreciated if someone knows how do to that!
PROBLEM: I must code a program that will store some numbers, but I don't know how much numbers there will be! What can I do?
I was wondering if I could use timing to get things done. I mean, if 5 secs has passed and there is no input of data, then start processing these numbers. It would work, but I couldn't code this. Can someone help, please?
1) first solution:
You can ask the user to enter the number of desired element at the beginning.
2) second solution:
Keep scan numbers till you get EOF from the user. and store the input number into a linked list or a dynamically allocated array (resize your array size withe the realloc)
3) third solution
keep scan numbers with a timeout. If there is no input during the timeout then the program will consider that the user have finished input numbers and then the program stop reading from stdin. The input numbers could stored into linked list or dynamic array as indicated in the second solution. Use select() with the scanf() in order to add the timeout behaviour as indicated in this answer

Problems to solve using control instructions in C for practice

Need some good problems which students can think of and apply their own logic to solve them using control instructions only. The topics covered until now are basic, not even arrays are done yet. But, I want students to be perfect before proceeding to higher topics.
I tried searching for some example problems, none were as I expected / they were the ones which I already knew.
Some of which I know:
Write a program to find out the value of a^b without using built in functions.
Write a program to find out Armstrong numbers between a range.
Write a program to print binary equivalent of a number in reverse order (since arrays are not yet done, just simple logic to print the remainder and divide the number further)
Count all -ve, +ve and 0 numbers entered by user until user wishes to terminate the program.
Write a program to display all divisors of a given number.
Write a program to find if the given number is prime or not.
Check if the given number is odd or even.
Need more good logically interesting problems which would help students to build their problem solving capability.
Thanks.
PS: Please forgive me if this question is vague or not to the point coz this question has scope for vast answers and I cannot accept a single answer, I guess?
Check if number is a palindrome (1234554321)
Rewrite a function using write() to print a number in the console (similar to printf("%d", ...))
A function that writes all combinations of 2 digits starting from 12 to 89, not allowing twice the same digit, nor a different order (12, 13, ..., 19, 23, 24... : skipping 21 because it's done with 12)
A function that write all combinations of n digits (n given as a parameter from 1 to 9) with the same rules (without using arrays)
Print first 33 terms of Fibonacci-Series
Write factorial of n being input from keyboard on console.
Find hours,minutes,seconds from given seconds.(305 s = 5m + 5s ....)
Calculate dot-product and cross-product of two 2D vectors.
Find the intersecting point of two lines(m=slope, (x0,y0)=a point for each line)
Calculate sin(pi/4) with using series expansion
Print the minimum of values given from keyboard on screen.
Simulate **and** , **or** and **xor** gates.
Find projection of a vector(3D) on another vector.
Find area of a polygon(2D)
Calculate the integral of x-square between x=0 and x=3 using integration by trapezoidal rule
Find roots of: (x-square) plus (two times x) plus (one) equals (zero)

C programming language, shortest path

I'm am writing a code to find the shortest distance between two points. My code is working perfect till now. I mean it finds the distance and the path that they should pass. I need to print this information, but I should make a print function. The way it works is something like that: For example initial point is 4 and final is 13.
I should come up with an algorithm that check their intermediate points. Let's say between 4 & 13 there is point : 7
4--7--13 Now I need to check every point between them like:
4--6--7--9--13 To be more specific it will check if there is a point between 4-6 and 6-7 and 7-9 and 9-13. So next in the next iteration it may be formed another list like:
4--2--6--7--5--9--17--13 Now let's say that there will not be any intermediate value between them. And that is what I should print. I really would appreciate any help, suggestion that you may give to me
The Warshall-Floyd algorithm (used by the OP), has a version which is able to determine the path in addition to the distance between nodes of a graph:
Floyd-Warshall algorithm with path-reconstruction
However, it must be noted that this is not the best possible algorithm to solve the shortest-path problem.
This sounds like recursion would be the best way to do this. If it already can find the shortest path, im assuming you have a function written to find the shortest path between 2 points. Maybe you could recursively break down the list, find the shortest path and append that point to a list.
Edit, sorry i misread your question, you need to find the midpoint. Pass a recursive function the whole list of points and find a midpoint. If one exists, add it to a list. If there is no midpoint dont append anything. Continue calling this function until you come to the base case, which should be 1 or 2 points in the list

How can I find all possible combinations of a string? Using CUDA

I am trying to speed up my algorithm by using CUDA to find all possible combination of a string. What is the best way I can achieve this?
example:
abc
gives:
a
b
c
ab
ac
bc
i have nothing so far. i am not asking for code. i am just asking for the best way to do it? an algorithm? a pseudocode? maybe a discussion?
The advantage to using CUDA is massive parallelism with potentially thousands of threads with little overhead. To that end, you have to figure out a way to divide the problem into small chunks without relying too much on communication between the threads. In this problem you have n characters and each can be either present or absent in each output string. This yields 2^n total output strings. (You've left off the empty string and the original string from your list...if that's the desired result then you have 2^n - 2 total output strings.)
In any event, one way you can divide up the work of creating the strings is to assign each potential output string a number and have each thread compute the output strings for a certain range of numbers. The mapping from number to output string is easy if you look at the binary representation of each number. Each binary digit in an n-bit number corresponds to a character in the string of length n. Thus, for your example, the number 5 or 101 in binary maps to the string "ac". The strings you listed would be created by computing the mappings for numbers from 1 to 6 as follows:
1 c
2 b
3 bc
4 a
5 ac
6 ab
You could compute 7 to get abc or 0 to get the empty string if desired.
Unless you're doing this for words longer than a dozen or so characters, I'm not sure this will be that much faster though. If you're doing it for words longer than 25 or so characters you might start running into memory constraints since you'll be wrangling hundreds of megabytes.
I will be very, very surprised if CUDA is the right solution to this problem.
However, I would write a kernel to find all substrings of length n, and launch the kernel in a loop for each value of n from 0 to the length of the string. Thus, each thread in a kernel will have exactly the same instructions (no threads will sit around idle while others finish).
Each thread will "find" one substring, so you might as well have thread i find the substring starting at index i in the string. Note that each substring length requires a different number of threads.
so, for n=1:
thread 0: a
thread 1: b
thread 2: c
and for n=2:
thread 0: ab
thread 1: bc

Resources