my program does not runing and does not show any error, could you show which mis
ID: 3686620 • Letter: M
Question
my program does not runing and does not show any error, could you show which mistake a did please?
Design and write a computer program as a Win32 console application in C to store a list of names (the last name first) and ages in parallel arrays, and then later to sort them into alphabetical order, keeping the ages with the correct names.Your program shall receive information from the user as a command-line argument
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LEN 100
#define NAME_LEN 41
void data_sort(char *names[LINE_LEN],int ages[LINE_LEN])
{
char *temp;
int i = 0;
int j = 0;
int temp1;
while (i < 100)
{
int j = i+1;
while (j < 100)
{
if (strcmp(names[i],names[j] ) > 0 )
{
/* temp = names[i];
names[i] = names[j];
names[j] = temp;*/
temp1 = ages[i];
ages[i]= ages[j];
ages[j]= temp1;
j += 1;
}
i+= 1;
}
i = 0;
while (i < 100)
{
printf("%s %s %d ", names[i],ages[i]);
i += 1;
}
}
}
/* Name main */
int main(int argc, char *argv[])
{
/*declare variable*/
FILE *inp;
char *alpha[LINE_LEN];
int status;
char name[LINE_LEN][NAME_LEN];
char temp_first[NAME_LEN];
int age[LINE_LEN];
int counter=0;
int total_count;
/*if there is no file*/
if (argc < 2)
{
printf(" name of student. ");
exit (0);
}
else if (argc>=2)
{
inp = fopen (argv[1], "r");
if (inp == NULL)
{
printf("%s ","NO SUCH FILE EXIST");
exit(0);
}
else
{
status = fscanf(inp,"%s %s %d",name[counter],temp_first,&age[counter]);
counter=0;
while (status!= EOF && counter<100)
{
//for (i=0; i< 100; ++i)
strcat(name[counter]," ");
strcat(name[counter],temp_first);
++counter;
status = fscanf(inp,"%s %s %d",name[counter],temp_first,&age[counter]);
//printf(" %s %s %d ",name[i],temp_first,age[i]);
}
total_count = counter;
for (counter= 0; counter < total_count; counter ++)
{
alpha[counter]=name[LINE_LEN];
}
data_sort(&alpha[counter],&age[counter]);
fclose(inp);
}
printf(" %s %s %d ",name[counter],temp_first,age[counter]);
}
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LEN 100
#define NAME_LEN 41
void data_sort(char names[][NAME_LEN],int ages[], int n)
{
char temp[20];
int i = 0;
int j = 0;
int temp1;
while (i < n)
{
j = i+1;
while (j < n)
{
if (strcmp(names[i],names[j] ) > 0 )
{
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
temp1 = ages[i];
ages[i]= ages[j];
ages[j]= temp1;
}
j += 1;
}
i+= 1;
}
i = 0;
while (i < n)
{
printf("%s %d ", names[i],ages[i]);
i += 1;
}
}
void print(char names[][NAME_LEN],int ages[], int n){
int i;
for(i=0; i<n; i++){
printf("%s %d ",names[i], ages[i]);
}
}
/* Name main */
int main(int argc, char *argv[])
{
/*declare variable*/
FILE *inp;
int status;
char name[LINE_LEN][NAME_LEN];
char temp_first[NAME_LEN];
int age[LINE_LEN];
int counter=0;
/*if there is no file*/
if (argc < 2)
{
printf(" name of student. ");
exit (0);
}
else if (argc>=2)
{
inp = fopen (argv[1], "r");
if (inp == NULL)
{
printf("%s ","NO SUCH FILE EXIST");
exit(0);
}
else
{
status = fscanf(inp,"%s %s %d",name[counter],temp_first,&age[counter]);
counter=0;
while (status!= EOF && counter<100)
{
//for (i=0; i< 100; ++i)
strcat(name[counter]," ");
strcat(name[counter],temp_first);
++counter;
status = fscanf(inp,"%s %s %d",name[counter],temp_first,&age[counter]);
//printf(" %s %s %d ",name[i],temp_first,age[i]);
}
data_sort(name,age, counter);
fclose(inp);
//printing data
print(name, age, counter);
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.