5

Iterate in a table

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define TABSIZE 4
  5.  
  6. main() {
  7.     float tab[ TABSIZE ];
  8.     float *pf;
  9.     int i;
  10.  
  11.  
  12.     printf("Enter 4 reals: \n");
  13.  
  14.     i = 0;
  15.     while ( i < TABSIZE ) {
  16.         printf("%d: ", i+1);
  17.         switch( scanf("%f", &tab[i])) {
  18.             case 0:
  19.                 exit(1);
  20.             case 1:
  21.                 ++i;
  22.                 break;
  23.             default:
  24.                 scanf("%*s");   /* swallow */
  25.                 break;
  26.         }
  27.     }
  28.  
  29.     printf("You have entered (in reverse order): \n");
  30.  
  31.     /* iteration with an index */
  32.     for ( i = TABSIZE-1; i >= 0; i-- )
  33.         printf("%d: %f\n", i+1, tab[ i ]);
  34.  
  35.     printf("Here are their addresses in memory: \n");
  36.  
  37.     /* iteration with a pointer */
  38.     for ( pf = tab; pf < tab+TABSIZE; pf++ )
  39.         printf("%f\t-> 0x%x\n", *pf, (unsigned)pf);
  40.  
  41.     exit(0);
  42. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].