5 Replies - 225 Views - Last Post: 20 April 2013 - 06:03 AM Rate Topic: -----

#1 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 21-March 13

Printing an Array

Posted 19 April 2013 - 01:52 PM

Quick question,

How do I print a 1D array?

Here's what I have:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
 
int main( void)
{
  double x[4] = { -2, -1, 2, 0 };

  return 0;
}


What code would I have to implement to print -2, -1, 2, 0?
Is This A Good Question/Topic? 0
  • +

Replies To: Printing an Array

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Printing an Array

Posted 19 April 2013 - 02:12 PM

Any of the 3 loop constructs could be used (for-loop, while-loop, or do-while-loop).

for(/* initialization */; /* condition */; /* increment/decrement */) { }

while(condition) { }

do
{
    // ...
}
while(condition)


I would think the for-loop would be your best bet. I would also save the size of the array in a constant variable for C++ or a macro for C:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define SIZE 4
 
int main( void)
{
  double x[SIZE] = { -2, -1, 2, 0 };
  int index;
  
  for(index = 0; index < SIZE; ++index)
  {
    /* do something */
  }

  return 0;
}


This post has been edited by vividexstance: 19 April 2013 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 21-March 13

Re: Printing an Array

Posted 19 April 2013 - 06:26 PM

Thanks! One question, what do I put as the print statement?


*** EDIT ***
No need to quote post above your reply

This post has been edited by GunnerInc: 19 April 2013 - 11:04 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1347
  • View blog
  • Posts: 4,638
  • Joined: 19-February 09

Re: Printing an Array

Posted 19 April 2013 - 06:59 PM

You can use printf.

Lesson 8: Arrays in C

How do you access each element of the array?
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Printing an Array

Posted 19 April 2013 - 07:43 PM

The double type takes a "%lf" as a format specifier.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,573
  • Joined: 25-December 09

Re: Printing an Array

Posted 20 April 2013 - 06:03 AM

Quote

The double type takes a "%lf" as a format specifier.

This is only true with the scanf() family of functions.

The printf() family of functions use the "%f" format specifier, unless you are using a compiler that is compiling as a C11 compliant program. Earlier versions of the standard only accepted the "%f" format specifier for all floating point numbers.

Note: I'm not sure if this feature was changed in C99 or C11, but the current standard C11 now uses "%f" for float and "%lf" for double with printf().

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1