Reputation: 19
I need to print multiple arrays side by side. how do I do that in c? like I have these arrays
const char* intel[18];
intel[0] = "i9-9900K = 599$";
intel[1] = "i9-9900 = 549$";
intel[2] = "i7-9700K = 499$";
intel[3] = "i7-9700 = 399$";
intel[4] = "i5-9600K = 329$";
intel[5] = "i5-9600KF = 259$";
intel[6] = "i5-9600 = 249$";
intel[7] = "i5-9400F = 200$";
intel[8] = "i5-9400 = 219$";
intel[9] = "i3-9100";
intel[10] = "i9-10900K";
intel[11] = "i9-10900F";
intel[12] = "i7-10700K";
intel[13] = "i7-10700";
intel[14] = "i5-10600K";
intel[15] = "i5-10400";
intel[16] = "i5-10400F";
intel[17] = "i3-10100";
const char* AMD[12];
AMD[0] = "Ryzen-5950X";
AMD[1] = "Ryzen-5900X";
AMD[2] = "Ryzen-5700X";
AMD[3] = "Ryzen-5600X";
AMD[4] = "Ryzen-3950X";
AMD[5] = "Ryzen-3900x";
AMD[6] = "Ryzen-3800x";
AMD[7] = "Ryzen-3700x";
AMD[8] = "Ryzen-3600x";
AMD[9] = "Ryzen-3600";
AMD[10] = "Ryzen-3400G";
AMD[11] = "Ryzen-3200G";
how should i print them side by side in the console
Upvotes: 1
Views: 310
Reputation: 50047
I suggest the following:
#include <stdio.h>
#include <string.h>
#define GREATEST(x, y) ((x) > (y) ? (x) : (y))
#define ARRAY_COUNT(array_var) (sizeof(array_var)/sizeof(array_var[0]))
#define FIND_MAX_STRLEN_IN_ARRAY(array_var, index_var, maxlen_var) \
maxlen_var = 0; \
for(index_var = 0 ; index_var < ARRAY_COUNT(array_var) ; ++index_var) \
maxlen_var = GREATEST(maxlen_var, strlen(array_var[index_var]))
int main(int argc, char **argv)
{
int i, max_intel_length, max_AMD_length;
const char* intel[] = {"i9-9900K = 599$", "i9-9900 = 549$", "i7-9700K = 499$",
"i7-9700 = 399$", "i5-9600K = 329$", "i5-9600KF = 259$",
"i5-9600 = 249$", "i5-9400F = 200$", "i5-9400 = 219$",
"i3-9100", "i9-10900K", "i9-10900F",
"i7-10700K", "i7-10700", "i5-10600K",
"i5-10400", "i5-10400F", "i3-10100"};
const char* AMD[] = {"Ryzen-5950X", "Ryzen-5900X", "Ryzen-5700X",
"Ryzen-5600X", "Ryzen-3950X", "Ryzen-3900x",
"Ryzen-3800x", "Ryzen-3700x", "Ryzen-3600x",
"Ryzen-3600", "Ryzen-3400G", "Ryzen-3200G"};
FIND_MAX_STRLEN_IN_ARRAY(intel, i, max_intel_length);
FIND_MAX_STRLEN_IN_ARRAY(AMD, i, max_AMD_length);
for(i = 0 ; i < GREATEST(ARRAY_COUNT(intel), ARRAY_COUNT(AMD)) ; ++i)
printf("%-*s\t%-*s\n",
max_intel_length, (i < ARRAY_COUNT(intel) ? intel[i] : ""),
max_AMD_length, (i < ARRAY_COUNT(AMD) ? AMD[i] : ""));
return 0;
}
This outputs
i9-9900K = 599$ Ryzen-5950X
i9-9900 = 549$ Ryzen-5900X
i7-9700K = 499$ Ryzen-5700X
i7-9700 = 399$ Ryzen-5600X
i5-9600K = 329$ Ryzen-3950X
i5-9600KF = 259$ Ryzen-3900x
i5-9600 = 249$ Ryzen-3800x
i5-9400F = 200$ Ryzen-3700x
i5-9400 = 219$ Ryzen-3600x
i3-9100 Ryzen-3600
i9-10900K Ryzen-3400G
i9-10900F Ryzen-3200G
i7-10700K
i7-10700
i5-10600K
i5-10400
i5-10400F
i3-10100
Done this way there are no hard-coded values for the array sizes which makes it easier and safer to add or remove elements as needed.
Upvotes: 2
Reputation: 1369
According to your needs, as I understand you need to allow user choose CPU and print it side by side.
I think it will be a good starting point to achieve want you to want.
There is no error handling you need to figure out how to do it, also you need to print equal-width columns because right now they are shifted a little bit.
#include <string.h>
#include <stdlib.h>
#define INTEL_SIZE 18
#define AMD_SIZE 12
#define BUFF_SZ 128
int main(int argc, char **argv)
{
char buff[BUFF_SZ];
int user_num = 0;
const char* intel[INTEL_SIZE];
intel[0] = "i9-9900K = 599$";
intel[1] = "i9-9900 = 549$";
intel[2] = "i7-9700K = 499$";
intel[3] = "i7-9700 = 399$";
intel[4] = "i5-9600K = 329$";
intel[5] = "i5-9600KF = 259$";
intel[6] = "i5-9600 = 249$";
intel[7] = "i5-9400F = 200$";
intel[8] = "i5-9400 = 219$";
intel[9] = "i3-9100";
intel[10] = "i9-10900K";
intel[11] = "i9-10900F";
intel[12] = "i7-10700K";
intel[13] = "i7-10700";
intel[14] = "i5-10600K";
intel[15] = "i5-10400";
intel[16] = "i5-10400F";
intel[17] = "i3-10100";
const char* AMD[AMD_SIZE];
AMD[0] = "Ryzen-5950X";
AMD[1] = "Ryzen-5900X";
AMD[2] = "Ryzen-5700X";
AMD[3] = "Ryzen-5600X";
AMD[4] = "Ryzen-3950X";
AMD[5] = "Ryzen-3900x";
AMD[6] = "Ryzen-3800x";
AMD[7] = "Ryzen-3700x";
AMD[8] = "Ryzen-3600x";
AMD[9] = "Ryzen-3600";
AMD[10] = "Ryzen-3400G";
AMD[11] = "Ryzen-3200G";
printf("Avaliable CPU list: \n");
for (int i = 0; i < INTEL_SIZE; i++) {
printf("%d) Intel CPU %-20s %s", i + 1, intel[i], i < AMD_SIZE ? " " : "\n");
if (i < AMD_SIZE) {
printf("%d) AMD CPU %s\n", i + 1, AMD[i]);
}
}
printf("Choose manufacturer AMD | intel\n");
fgets(buff, BUFF_SZ, stdin);
/* fgets leave '\n' character in the end we need to delete it */
buff[strcspn(buff, "\n")] = 0;
if (!strcmp(buff, "intel")) {
printf("Select CPU model from the list above\n");
memset(buff, 0, sizeof(buff));
fgets(buff, BUFF_SZ, stdin);
user_num = atoi(buff);
printf("intel [%s]\n", intel[user_num - 1]);
} else if (!strcmp(buff, "AMD")) {
printf("Select CPU model from the list above\n");
memset(buff, 0, sizeof(buff));
fgets(buff, BUFF_SZ, stdin);
user_num = atoi(buff);
printf("AMD [%s]\n", AMD[user_num - 1]);
} else {
printf("Unknown manufacturer\n");
exit(0);
}
return 0;
}
Upvotes: 0
Reputation: 1573
for(int i = 0; i < 18; i++) {
printf("%18s", intel[i]);
if( i < 12 ) {
printf("%s", amd[i]);
}
puts("");
}
Where the for
should run till i < lengthOfLongerArray
. if
condition is i < lengthOfShorterArray
. And %18s
means printing the array in a 18 characters width. I choose 18 cause the longest text in the first array counts 15 characters.
Upvotes: 0