Foundations of Programming Using C | Chapter 7: Arrays |
int x[10];
x[0] = 6; x[7] = 23; x[2] = x[0] + x[7];
#include <stdio.h> main() { int nums[5], i; printf("Enter %d numbers: ", 5); for (i = 0; i < 5; i++) scanf("%d", &nums[i]); printf("Thank you.\n"); printf("In reverse order, those numbers are:\n"); for (i = 5 - 1; i >= 0; i--) printf("%d", nums[i]); printf("\n"); }
#include <stdio.h> #define SIZE 5 main() { int nums[SIZE], i; printf("Enter %d numbers: ", SIZE); for (i = 0; i < SIZE; i++) scanf("%d", &nums[i]); printf("Thank you.\n"); printf("In reverse order, those numbers are:\n"); for (i = SIZE - 1; i >= 0; i--) printf("%d", nums[i]); printf("\n"); }
#include <stdio.h> #define SIZE 5 void fill(int x[]); void reverse(int x[]); main() { int nums[SIZE]; printf("Enter %d numbers: ", SIZE); fill(nums); printf("Thank you.\n"); printf("In reverse order, those numbers are:\n"); reverse(nums); } void fill(int x[]) { int i; for (i = 0; i < SIZE; i++) scanf("%d", &x[i]); } void reverse(int x[]) { int i; for (i = SIZE - 1; i >= 0; i--) printf("%d", x[i]); printf("\n"); }
#include <stdio.h> #define MAX 80 void backwards(char line[], int size); int getline(char line[]); main() { char input[80]; int numchars; printf("Enter a line to be reversed\n"); numchars = getline(input); printf("See if you can say this:\n"); backwards(input, numchars); } /* displays "size" characters from "line" in reverse order */ void backwards(char line[], int size) { int i; for (i = size - 1; i >= 0; i--) printf("%c", line[i]); printf("\n"); } /* Gets up to MAX characters from the user and stores them * in "line", returning the actual number of characters entered. * The newline at the end of the line is discarded, as are * any characters in excess of MAX on the input line. */ int getline(char line[]) { int n = 0; char c; while ('\n' != (c = getchar())) if (n < MAX) line[n++] = c; return n; }
int x[5] = { 10, -2, 3, 45, 6 };
int x[5]; x[0] = 10; x[1] = -2; x[2] = 3; x[3] = 45; x[4] = 6;
int x[5] = { 0 };