Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 26 (Programming in C) On a social network site (Facebook, MySpace, etc.

ID: 3820223 • Letter: Q

Question

Question 26 (Programming in C)

On a social network site (Facebook, MySpace, etc.) pairs of people can be friends. In this question each person is assigned a member number. If there are n members, each person is represented by a number between 0 and n-1.

The set of friends can be represented by an nxn two dimensional matrix. The entry F(i,j) is 0 if member i and member j are not friends and equals 1 if they are friends. We also assume that the diagonal elements (F(i, i)) are 0 since a person cannot be friends with him or herself. ( Note that the matrix is symmetric since if members i and j are friends, then F(i,j) = F(j,i) = 1.)

Now suppose the current social network is stored in a file named “friends.txt”. The first line of the file contains the number of members of the network and the ith line of the rest of the file represents the ith row of the matrix.

For example:

15
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Write a C program that:

Reads the social network from the file friends.txt into a two dimensional array.

Then reads a pair of numbers x and y (in the proper range)

If x and y are friends the program will output a message to that effect.

If x and y are not friends the program will determine whether x and y have any friends in common. It will output the list of mutual friends as well as the number of mutual friends (possibly 0).

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int N;
ifstream in;
in.open("friends.txt");
in>>N;
int Friend[N][N];
int a;
int i=-1,j=0;
while(!in.eof())
{
if(j==0)
i++;
in>>a;
Friend[i][j]=a;
j=(j+1)%N;
}
in.close();

for(i=0;i<N;i++)
{
cout<<" ";
for(j=0;j<N;j++)
{
cout<<Friend[i][j]<<" ";
}
}

int x,y;
cout<<" Enter the value of x and y:";
cin>>x>>y;

int Common[N],k=0;
if(Friend[x][y]==1)
cout<<" They re friends:";
else
{
for(i=x,j=y;i<N,j<N;i++,j++)
{
if(Friend[i]==Friend[j]==1)
{
Common[k++]=i;
}
}
cout<<" Common friends are given below: ";
for(int i=0;i<k;i++)
{
cout<<Common[i]<<" ";
}
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote