473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Storing numbers in an array then calling from a function

Hi, fairly new to C programming. I have been asked to generate a list of numbers and store the prime ones in an array so that they can be then later printed out in columns.

My generateprime function is working fine and i can list all the prime numbers i need but I need to know how to add the prime numbers found to an array,

eg
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     do {
  4.  
  5.         if the value is prime
  6.         {
  7.             printf(output the found prime to screen);
  8.             move on to test next numbers along
  9.         // i believe i need to add the found prime to the array here
  10.                                // but i will be using a function to pass the array through 
  11.                                // and im not sure what to do.
  12.  
  13.         }
  14.         else
  15.         {
  16.             move on to test next numbers along
  17.         }
  18.  
  19.     }while (the counter is less than requested amount);
  20.     //CALL Show prime function array
  21. }  

I am making a function which the program can call to list the numbers found in columns on a screen.

sort of like this
Expand|Select|Wrap|Line Numbers
  1.  
  2. void showprimes(int array[]) 
  3. {
  4.  printf(every 5 primes found for example a new line begins)
Im really just getting very confused. If someone could help me understand how to store the found primes into an array and then call them from a function I would be very grateful.
Jan 12 '07 #1
4 2669
Banfa
9,065 Expert Mod 8TB
One of the problems with storing something like primes in an array is that you have to know how many entries you will need when you create the array.

However you can get round this by allocating memory (function: malloc) and using a pointer to hold the address, you can then use this pointer as an array name (simply speaking). If you runout of space in your allocated array you can simply allocate more memory (function: realloc).

The all you need to do is have a variable that gives the next array index to write to, this would be initialised to 0, everytime you find a prime number you execute the following pseudo code

Expand|Select|Wrap|Line Numbers
  1. if (nextIndex == maxIndex)
  2. {
  3.     reallocate array
  4.     recalculate maxIndex
  5. }
  6.  
  7. array[nextIndex] = newPrimaryNumber;
  8. nextIndex++;
  9.  
Jan 12 '07 #2
Thanks for the reply. I dont know much about pointers. Im attempting to store 1000 prime numbers found in the array and then call it later using a function to display these 1000 in columns.
Jan 12 '07 #3
Banfa
9,065 Expert Mod 8TB
Pointers aren't that hard but if you have a defined limit (1000) then an array will do fine

the pseudo code I posted modifies to

Expand|Select|Wrap|Line Numbers
  1. if (nextIndex < 1000)
  2. {
  3.     array[nextIndex] = newPrimaryNumber;
  4.     nextIndex++;
  5. }
  6.  
printing is simple, by this time of course nextIndex contains the number of primes in the array (should be 1000)

Expand|Select|Wrap|Line Numbers
  1. for(temp = 0; temp<nextIndex; temp++)
  2. {
  3.     printf("%d ", array[temp]);
  4. }
  5.  
I will leave it to you to work out how in insert the new lines but you will probably want to make use of the % operator.
Jan 12 '07 #4
Thank you very much. I will look into it.
Jan 12 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
6
by: supercomputer | last post by:
I am using this function to parse data I have stored in an array. This is what the array looks like: , , , , , , , , , , , , , , , , , , , , , , , ] This is the code to parse the array:
5
by: GIMME | last post by:
My question is ... How do I sort an Array on numeric, not character values ? In the example below, after sorting the contents are 1,10,2,3 . How do I get the contents to be 1,2,3,10 ? ...
7
by: paladin.rithe | last post by:
Is it possible to store classes in an array? I am fairly new to PHP, and haven't found anything either way yet. I have a program that where you can have multiple notes attached to a ticket, which...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
17
by: Stubert | last post by:
I have a training module db that stores information about employees and what training they have carried our or need to carry out. One table in this database stores what training needs to be carried...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
14
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
0
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 project—planning, coding, testing,...

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.