Question:
C programming question?
2010-11-23 08:00:07 UTC
Write a MAIN function and two FUNCTIONS to compute and print the
prime elements of a two-dimensional 10X15 integer array A.
Within the MAIN function:
. Declare a two-dimensional 10X15 integer array A and initialize
it with random numbers between 100 and 500 inclusive.
. Declare also a one-dimensinal integer array B with a reasonable
size to hold the prime elements of the array A.
. Print the array A.
. Pass the arrays A and B to the FUNCTION1 as arguments and get
the return value(the size of B, the number of prime elements
of A stored in B) from the FUNCTION1.
. Pass the array B and the size of B(returned value from the
FUNCTION1) to the FUNCTION2 as arguments.
Within the FUNCTION1:
. Read array A elements and compute and store the prime elements
of A into the array B.
(An integer n>=2 is said to be prime
if and only if no integer k, 2<= k <= sqrt, divides n).
. Return the size of the array B(the number of the prime elements
stored in B) to the MAIN function.
Within the FUNCTION2:
. Print the array B.

Really need help with this
Four answers:
Mr Ed
2010-11-23 08:17:19 UTC
This is a simple task, for any language. What exactly do you need help with?



(You don't expect anyone to write it FOR you, do you?)
Chris C
2010-11-23 17:02:08 UTC
There aren't any magic tricks to programming. Programming is mostly being methodical in breaking down something into components, and figuring out how those components interact with each other or the end user (if necessary).

The basic way to start a C program out is to define the main() function:



#include



int main(int argc, char *argv[])

{

   /* "argc" contains the number of command line arguments */

   /* "argv[]" contains the actual command line arguments */

   /* "argc" is usually at least 1, because the first command */

   /* is the path to the program and the executable name of the program */

   /* So, argv[0] is usually the name of this program */



   return 0;

}



Continue on from there. If you have a question about other things, feel free to ask us here, or for some really brief help on C and C++, you can look here: www.cplusplus.com
husoski
2010-11-23 17:30:59 UTC
You can't pass arbitrary 2-D array arguments in C. At the very minimum, the number of columns must be declared. These should be symbolic constants, created with #define so that they can be used in array definitions (where the space is allocated) and in matching declarations (in arguments that refer to the array.)



Use rand() to get a random number between 0 and RAND_MAX (inclusive). This can be converted to another range in several ways, but two stand out. The cheap and easy (aka "cheesy") way:



n = 100 + rand() % (500 - 100 + 1);



The slow-but-accurate way:



n = (int)floor(100 + (500 - 100 + 1)*rand()*(1.0 / (RAND_MAX + 1.0)));



Use srand(time(NULL)) just once in main() to



The floor() function is defined in . The rand() and srand() functions are defined in . The time() function is defined in .



Here's a template to get you started. Only the #defines and prototypes are finished. I put includes in alphabetical order so I can see at a glance whether a given library is in use.



/* Template for C app */



/* Includes */



#include

#include

#include

#include



/* Constants */



#define A_ROWS 10

#define A_COLS 15

#define B_SIZE (A_ROWS*A_COLS)



/* Function prototypes: */



int FUNCTION1( int A[A_ROWS][A_COLS], int B[B_SIZE] );

int FUNCTION2( int B[B_SIZE], int B_Length );



/* Main */



int main( void )

{

/* TODO: Add main code here */

return 0;

}



/* FUNCTION1: Generate data */



int FUNCTION1( int A[A_ROWS][A_COLS], int B[B_SIZE] )

{

/* TODO: Add FUNCTION1 code here */

}



/* FUNCTION2: Display data: */



int FUNCTION2( int B[B_SIZE], int B_Length )

{

/* TODO: Add FUNCTION2 code here */

}
Vira
2010-11-23 17:02:09 UTC
It pretty much tells you what to do. Just write a statement for each step?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...