Can anyone help me fix this code to use it in a windows 8.1 computer. Im using V
ID: 3686273 • Letter: C
Question
Can anyone help me fix this code to use it in a windows 8.1 computer. Im using Visual Studio 2015. The code below was given to me from a Macintosh computer, so I really dont know how to fix this. Please help!!
#include <stdio.h>
#include <string.h>
void readNames(char Names[][10], int num)
{
for(int i = 0; i < num; i++)
{
printf("Enter the name of student %i: ", i+1);
scanf("%s", Names[i]);
}
}
void sortNames(char Names[][10], int num)
{
char temp[10];
for (int i = 0; i < num; i++)
for (int j = 0; j < num - 1; j++)
if (strcmp(Names[j], Names[j + 1]) > 0)
{
strcpy(temp, Names[j]);
strcpy(Names[j], Names[j + 1]);
strcpy(Names[j + 1], temp);
}
}
int main()
{
char Names[10][10];
int numOfStudents;
printf("Enter the number of students to read in: ");
scanf("%i", &numOfStudents);
readNames(Names, numOfStudents);
sortNames(Names, numOfStudents);
printf("The names after sorting are: ");
for(int i = 0; i < numOfStudents; i++)
printf("%s ", Names[i]);
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
void readNames(char names[15][10], int num)
{
char tname[15][8];
printf("Enter the name", );
for (int i = 0; i < num; i++)
{
scanf("%s", names[i]);
strcpy(tname[i], names[i]);
}
}
void sortNames(char names[15][10], int num)
{
char temp[10];
for (int i = 0; i < num - 1 ; i++)
{
for (int j = i + 1; j < num; j++)
{
if (strcmp(names[i], names[j]) > 0)
{
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
printf("The names after sorting are: ");
for (int i = 0; i < num; i++)
{
printf("%s ", names[i]);
}
}
int main()
{
char names[15][10];
int i,num;
printf("Enter the number of students to read in: ");
scanf("%d", &num);
readNames(names,num);
sortNames(names,num);
}