In: Computer Science
Can someone tell me how to fix warning msg in my code of C ++?
I got run-time error for this question please help me asap!
Errors are: In function 'void bfs(int, int)': warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int j = 0; j < adj[pppp].size(); j++){ ^ In function 'int main()': warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d %d", &a, &q, &c, &N, &m); ^ warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d", &fir, &bbb); ^
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
int min(int n,int arr[]){
int m=arr[0];
for(int i=1;i<n;i++)
if(m>arr[i])
m=arr[i];
return m;
}
using namespace std;
const int Max_max = 404040;
const int INF = 1e7;
int sum[Max_max];
int a,q,c,N,m,fir;
vector<int> adj[Max_max];
void bfs(int money, int start_node){
bool hello[Max_max];
for(int i=0; i<=N; i++)
hello[i] = false;
queue<int> q;
q.push(start_node);
hello[start_node] = true;
int distance = 1;
do{
int size = q.size();
for(int i = 0; i < size; i++){
int pppp = q.front(); q.pop();
for(int j = 0; j < adj[pppp].size(); j++){ //error undeclared identifier 'ppp_city'
int next = adj[pppp][j];
if(hello[next])
continue;
sum[pppp] += money * distance;
hello[next] = true;
q.push(next);
}
}
distance++;
} while (q.size() > 0);
}
int main(){
scanf("%d %d %d %d %d", &a, &q, &c, &N, &m);
for(int i=0; i < m; i++){
int aaa, bbb;
scanf("%d %d", &fir, &bbb);
adj[aaa].push_back(bbb);
adj[bbb].push_back(aaa);
}
bfs(a,1);
bfs(q,2);
bfs(c,N);
int fin = INF;
for(int i = 1; i <= N; i++)
fin = min(fin, sum); //error no matching function to call min
printf("%d",fin);
return 0;
}
If you have any doubts, please give me comment...
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
int min(int n, int arr[])
{
int m = arr[0];
for (int i = 1; i < n; i++)
if (m > arr[i])
m = arr[i];
return m;
}
using namespace std;
const int Max_max = 404040;
const int INF = 1e7;
int sum[Max_max];
int a, q, c, N, m, fir;
vector<int> adj[Max_max];
void bfs(int money, int start_node)
{
bool hello[Max_max];
for (int i = 0; i <= N; i++)
hello[i] = false;
queue<int> q;
q.push(start_node);
hello[start_node] = true;
int distance = 1;
do
{
int size = q.size();
for (int i = 0; i < size; i++)
{
int pppp = q.front();
q.pop();
for (size_t j = 0; j < adj[pppp].size(); j++)
{ //error undeclared identifier 'ppp_city'
int next = adj[pppp][j];
if (hello[next])
continue;
sum[pppp] += money * distance;
hello[next] = true;
q.push(next);
}
}
distance++;
} while (q.size() > 0);
}
int main()
{
int result = scanf("%d %d %d %d %d", &a, &q, &c, &N, &m);
if(result!=5)
printf("input error!\n");
for (int i = 0; i < m; i++)
{
int aaa, bbb;
result = scanf("%d %d", &fir, &bbb);
adj[aaa].push_back(bbb);
adj[bbb].push_back(aaa);
}
bfs(a, 1);
bfs(q, 2);
bfs(c, N);
int fin = INF;
for (int i = 1; i <= N; i++)
fin = min(fin, sum); //error no matching function to call min
printf("%d", fin);
return 0;
}