// A program that takes integers as command
// line arguments, jumbles them a little
// then types them back out
// Marc Chee (marc.chee@unsw.edu.au), July 2019
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMS 10
void read_args(int nums[MAX_NUMS], char *arguments[], int argCount);
void print_nums(int nums[MAX_NUMS], int numCount);
void swap_nums(int *num1, int *num2);
void jumble(int nums[MAX_NUMS], int numCount);
int main(int argc, char *argv[]) {
int numbers[MAX_NUMS];
read_args(numbers, argv, argc);
jumble(numbers, argc - 1);
print_nums(numbers, argc - 1);
return 0;
}
// Read the command line arguments, ignoring the program name
// and convert them into integers, placing them in the array nums
void read_args(int nums[MAX_NUMS], char *arguments[], int argCount) {
int i = 0;
while (i < MAX_NUMS && i < argCount - 1) {
nums[i] = strtol(arguments[i + 1], NULL, 10);
i++;
}
}
// print out a list of numbers to standard output
void print_nums(int nums[MAX_NUMS], int numCount) {
int i = 0;
while (i < MAX_NUMS && i < numCount) {
printf("%d ", nums[i]);
i++;
}
}
void swap_nums(int *num1, int *num2) {
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
void jumble(int nums[MAX_NUMS], int numCount) {
int i = 0;
while (i < MAX_NUMS && i < numCount) {
int j = i * 2;
if (j < MAX_NUMS && j < numCount) {
swap_nums(&nums[i], &nums[j]);
}
i++;
}
}
Resource created Tuesday 09 July 2019, 03:30:55 AM, last modified Tuesday 16 July 2019, 02:17:32 AM.
file: jumbler.c