Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:51:49+00:00 2026-06-05T19:51:49+00:00

With your help I could take the input from a text file (input.txt) where

  • 0

With your help I could take the input from a text file (input.txt) where lines consisted of city1 city2 distance … and write the names of cities in a matrix without repetition. According to this matrix I wrote a piece of code to add their distances in a adjacency matrix. But the output looks weird I mean, it is not correct. I guess in my code below should be something missing or wrong. Any little help is highly appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]){
int i=1,j, state=0, k, dist,x=0,y=0;

   int** myMat;
   char *city1, *city2, **matnames;
   FILE* p;
    city1 = (char*) malloc(sizeof(char));
    city2 = (char*) malloc(sizeof(char));
    matnames = (char**) malloc(sizeof(char*));
    myMat = (int**) malloc(sizeof(int*)*4);             
p = fopen(argv[1],"r");

/************************************************************/
    matnames[0] = (char*) malloc(sizeof(char));
    matnames[1] = (char*) malloc(sizeof(char));
    matnames[2] = (char*) malloc(sizeof(char));
    matnames[2] = NULL;
    fscanf(p, "%s %s %d", city1, city2, &dist);
        strcpy(matnames[0],city1);                  
        strcpy(matnames[1],city2);
/************************************************************/  
    for(i=0;i<3;i++){
        myMat[i] = (int*) malloc(sizeof(int));
    }
    myMat[1][2] = dist;         /* the first two distances placed at matnames */
    myMat[2][1] = dist;

/************************************************************/  
while( fscanf(p,"%s %s %d",city1,city2, &dist) != EOF){                 
        for(j=0; matnames[j]!=NULL; j++){                   
            if( strcmp(matnames[j], city1) != 0){
                 state++;
                }               
        }       
        if(state  == j){
            matnames = realloc(matnames, sizeof(char*)*(j+3));
            matnames[j] = (char*) malloc(sizeof(char));
            strcpy(matnames[j], city1);
            matnames[j+1] = (char*) malloc(sizeof(char));
            matnames[j+1] = NULL;   
            }
            state = 0;
        for(j=0; strcmp(matnames[j], city1) != 0;j++){ 
                x++;                   /* "x" finds the city1 indeks from matnames*/
        }

        for(k=0; matnames[k] != NULL;k++){                  
                if( strcmp(matnames[k], city2) != 0){
                    state++;
                }               
        }
        if(state == k){
            matnames = realloc(matnames, sizeof(char*)*(k+4));
            matnames[k] = (char*) malloc(sizeof(char));
            matnames[k+1] = (char*) malloc(sizeof(char));
            strcpy(matnames[k], city2); 
            matnames[k+1] = NULL;   
            }
        state = 0;
    /* till to here the names of cities are placed in matnames without repetion*/
        for(j=0; strcmp(matnames[j], city2) != 0;j++){ 
                y++;             /* "y" finds the city2 indeks from matnames*/
        }
        /****** the problem should be in this part */
        myMat = realloc(myMat,sizeof(int*)*(k+3));          
        for(i=2;i<k+2;i++){
            myMat[i] = (int*) malloc(sizeof(int));
        }
        myMat[x][y] = dist;
        myMat[y][x] = dist;
        x=0; y=0;
}   
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:51:52+00:00Added an answer on June 5, 2026 at 7:51 pm

    you allocate 1 char to city1 and city2 (malloc with sizeof(char)). Unless your cities all have just one character, the fscanf will go out of bounds.

    Try again with city1 = (char*) malloc(1024) so a city can be a bit longer.

    matnames is not very obvious what you want it to be, but you allocate it 1 sizeof(pointer), i.e. 4 bytes. Then you do matnames[0] = …, matnames[1] = …
    matnames[1] is already overflowing the memory you allocated. So you’re writing in random places.

    The C-language is very forgiving regarding memory allocations, but the results are completely unpredictable. Be sure your malloc enough space, and consider other languages when possible. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which take data from 5 huge table, could you please
I worked in android app that read large text file from server (20 MB)
Seeking your help to get off .epub(s) sea in iPhone world... I am trying
Need your help to get the max of CAP_PRICE based on certain criteria in
I need your help to solve a silly problem. I have 2 tables in
I need your help. I have a C executable called generator.out that is a
I need your help with making comments show below my articles. At the moment
Please i need your help with how to alternate background colours for my comments
I need your help with the textarea of my form fields. I have the
I need your help to validate what is causing this error. This is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.