LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2013, 02:29 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
VLA array style


I'm a newbie to C programming, and wonder if anyone can tell me if the following code I've written for a text problem is correct in style, or can it be improved to look "professional".
Problem is:"Make 3 sets of 5 doubles stored as array.
Compute average of each row using VLA."
Particular question regards my style of passing in additional argument to function prototype so that row#
can be passed to function.
Code:
#include <stdio.h>
#define ROWS 3
#define COLS 5

void avg_row(int ,double ar[*],int );   //fn prototype
int main(void)
{
int i= ROWS,j=COLS;
double arr[i][j];

    for(i=0;i<ROWS;i++)                 //initialize array
        {
        for(j=0;j<COLS;j++)
            {
            puts("Enter a double value");
            while(scanf("%lf", &arr[i][j])!=1 && getchar()!='\n')
                {
                printf("Retry: Enter a double value!\n");
                }
            }
        }
   for(i=0;i<ROWS;i++)                  //1-D array function calls
    {
     avg_row(COLS,arr[i],i);
    }
return 0;
}
}

void avg_row(int n,double ar[n],int m)  //fn definition
{
double linetotal=0.0;

        for(int i=0;i < n; i++)         //sum array rows
            {
            linetotal += ar[i];
            }
            printf("Average for row %d is %.2f\n",m+1,linetotal/n);
}
Thanks for any help

Last edited by atlantis43; 11-11-2013 at 02:30 PM. Reason: typo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
e.g., BSD style (Slackware) vs. SystemV style startup scripts haertig Slackware 5 01-03-2009 10:52 PM
Compiling kernel Debian style or Native style ? Raynus Debian 1 06-16-2008 06:56 AM
How to know if I am using mbox-style or maildir-style? Niceman2005 Linux - General 1 09-23-2005 12:08 PM
VIM-style wrapping to OpenOffice style schmmd Linux - Software 1 12-21-2004 06:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration