Thread: arrays and functions

  1. #1
    Registered User
    Join Date
    May 2009

    arrays and functions

    ok so im trying to get my head around all this c stuff..
    if i had inputs from a user say...
    3 4 5
    2 4 7
    and i wanted to compute the sqrt of each row summed, plus the number of loops occuring, how would i add 3 parallel arrays and write a loop to read in the values?
    so far i have this code but it isnt in the format of an array or function. i have tried to do it but im not getting the correct output
    main (int argc, char **argv){
    double x, y, z, a, b;
    int n;
    	scanf("%lf %lf %lf", &x, &y, &z);
    while(scanf("%lf %lf %lf", &x, &y, &z)==3){
    	if (n>0){
    printf("%f, %f", a,b);
    	return 0;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    I'm not sure what your question is, exactly, because "adding 3 parallel arrays" is neither necessary nor desirable if the problem is computing the square root of the sum of a row of a matrix.

    As to input, do you know ahead of time how many numbers to read in? That will tell you how your loop will run. If you don't, then the first thing you should input is how many numbers you are going to read in so that you can write the loop.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    The first thing you will want to do then is put this stuff in an array:
    int array[3];
    scanf("%lf %lf %lf", &array[0], &array[1], &array[2]);
    Which makes it much easier to iterate thru using a loop:
    for (i=0;i<3;i++) printf("%d\t",array[i]);
    Meaning your task will be even easier with a two dimensional array, to hold multiple arrays. If you do not know how many arrays there will be, you use a **pointer to an array of pointers (each of those is to an array). But work one step at a time or you will waste time writing tons of code that does not work.

    Do look at the program I gave you earlier, it may seem complicated right now but there are lots of examples of the correct syntax for performing the kind of tasks common to handling "matrices" (multi dimensional arrays).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    thanks for the quick response....however that previous code u gave me is alot more complicated
    if i put all of this in a function, how could i declare an array and alter the reading loop ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM