/*
* program to show how to change numbers to strings
* it generates pseudo-random numbers to do this
*
* Matt Bishop, ECS 36A
*
* April 22, 2025
* -- original version
*/
#include <stdio.h>
#include <stdlib.h>
/*
* macros
*/
#define MAX 32767 /* numbers between 0 and 32767 incusive */
/*
* main routine
*/
int main(void)
{
char buf[10][1024]; /* array of buffers to hold strings */
int i; /* counter in for loops */
int x, y; /* numbers to be added */
int sum; /* sum of numbers to be added */
/*
* generate the strings and put them into the array
*/
for(i = 0; i < 10; i++){
/* generate the numbers to be added */
x = rand() % MAX;
y = rand() % MAX;
/* add them */
sum = x + y;
/* convert to string and put it into the array */
sprintf(buf[i], "The sum of %5d and %5d is %5d", x, y, sum);
}
/*
* now print the strings
*/
for(i = 0; i < 10; i++)
printf("%s\n", buf[i]);
/*
* all done!
*/
return(0);
}
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |