When i try to run this code, it compiles without any error, but i wanted that it should display the string and int array that it takes as input. Instead, after giving one input, I am presented with a list of zeroes and program terminates.
however,when i removed the line containing k[j] from both for loops, it worked.
What am I doing wrong? Pardon if asked something stupid as I'm a novice, please help...
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int t,count = 0;
string state[t];
int k[t];
cin>>t;
for (int j=0; j<t; j++)
{
getline(cin, state[j]);
cin>>k[j];
}
for (int j=0; j<t; j++)
{ cout<< state [j]<<'\t'<<k[j];
cout<<endl;
}
return 0
}
You declare both arrays k[] and state[] with t wich is undefined at this moment.
t should be initiated with a valid value beforehand!
Related
I need to set the last element inside an array by multiply the last "i" with it self like this. but when i try to do i*i, i is undefined. also, when i try to print the result, cout is undefined.
void firstArray(void)
{
int MyArray[10] ;
for (unsigned int i=0; i<10; ++i)
{
MyArray[i] = i;
}
MyArray [9] = i*i;
for (unsigned int i=0; i<10; ++i)
{
cout(MyArray[i]);
}
}
I tried to put MyArray [9] = i*i inside the loop; with a condition( and it whould work), but i cant use any if for this assignment.
also, I tried to put with System.out like in java before cout, but System is undefined.
what do i need to change to make it work?
Ok, so first of all, if you want "i" outside of your loop, you need to initialize "i" outside of your loop too.
... Unsigned int i; for(i =0; i<10; i++) ...
Now, "i" will be equal the last increment outside your loop.
Also, i suggest you to read the basics for c++, System dont exist in c++, instead its with std namespace.
There is two way to do that:
Using namespace std;
Inside the function, or by write "std::" before cout and:
Std::cout << MyArray[i]
Pay attention how i wrote it, you will find
How to do in the c++ website:
https://www.cplusplus.com/reference/iostream/cout/
#include <iostream>
#include <bits/stdc++.h>
#include <stack>
#include <algorithm>
#include <vector>
#include <iterator>
#define ll long long
using namespace std;
int main() {
ll t;
cin >> t;
while(t--) {
ll n,k;
cin >>n >>k;
vector<ll> arr;
for(int i=0; i<n; i++) cin >>arr[i];
vector <ll> freq;
for(int i=1;i<=k;i++) freq.push_back(i);
ll min=n,val,diff=0,max=0;
for(int i=0;i<k;i++){
ll c=count(arr.begin(),arr.end(),freq[i]);
if(min>c)
{min=c;
val=freq[i];
}
}
if(min==0) max=n;
else{
vector<ll> diffarr;
diffarr.push_back(0);
for(int i=0;i<n;i++)
{
if(arr[i]==val){
diffarr.push_back(i);
}
}
for(int i=0;i<min;i++)
{
diff=diffarr[i+1]-diffarr[i];
if(max<diff) max=diff;
}
}
cout <<max <<endl;
}
return 0;
}
The code works fine when input is not taken but as soon as I take input it throws a SIGSEGV error
I have debugged it many times to get rid of all segmentation faults and all kinds of errors. Can someone pls look into it, that would be a gr8 help
the code is correct
No, it isn't. Start with these lines:
vector<ll> arr;
for(int i=0; i<n; i++) cin >>arr[i];
You declare an empty vector arr, and immediately try to write to element 0 in that vector.
But the vector is still empty, so there is no element 0 in it. On most STL implementations this will crash by trying to write to address 0.
Reading How to debug small programs is likely to benefit you. So will learning how to use a debugger.
i am getting 0xc0000005 error(access violation error), where am i wrong in this code?
i couldnt debug this error. please help me.
question is this
Formally, given a wall of infinite height, initially unpainted. There occur N operations, and in ith operation, the wall is painted upto height Hi with color Ci. Suppose in jth operation (j>i) wall is painted upto height Hj with color Cj such that Hj >= Hi, the Cith color on the wall is hidden. At the end of N operations, you have to find the number of distinct colors(>=1) visible on the wall.
#include<iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
for(int tt= 0;tt<t;tt++)
{
int h,c;
int temp = 0;
cin>>h>>c;
int A[h], B[c];
vector<int> fc;
for(int i = 0;i<h;i++)
{
cin>>A[i];
}
for(int j =0;j<h;j++)
{
cin>>B[j];
}
if(is_sorted(A,A+h))
{
return 1;
}
if(count(A,A+h,B[0]) == h)
{
return 1;
}
for(int i = 0;i<h;i++)
{
if(A[i]>=temp)
{
temp = A[i];
}
else
{
if(temp == fc[fc.size()-1])
{
fc[fc.size()-1] = B[i];
}
else
{
fc.push_back(B[i]);
}
}
}
}
}
There are several issues.
When reading values into B, your loop check is j<h. How many elements are in B?
You later look at fc[fc.size()-1]. This is Undefined Behavior if fc is empty, and is the likely source of your problem.
Other issues:
Don't use #include <bits/stdc++.h>
Avoid using namespace std;
Variable declarations like int A[h], where h is a variable, are not standard C++. Some compilers support them as an extension.
My implementation of selection sort does not work in case of j < n-2 or n-1 or n. What am I doing wrong?
Is there an online IDE that lets us put a watch for the control loops?
#include <stdio.h>
#define n 4
int main(void) {
int a[n]={4,3,2,1};
int j,min;
for(int i=0;i<n;i++){
min=i;
for(j=i+1;j<n-3;j++)
if(a[j]>a[j+1])
min=j+1;
if(min!=i){
int t=a[min];
a[min]=a[i];
a[i]=a[t];
}
}
for(int i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
I tried it here
Your code has indeed a strange limit on n-3, but it has also some other flaws:
To find a minimum you should compare with the current minimum (a[min]), not the next/previous element in the array
The code to swap is not correct: the last assignment should not be from a[t], but t itself.
Here is the corrected code:
int main(void) {
int a[n]={4,3,2,1};
int j,min;
for(int i=0;i<n;i++){
min=i;
for(j=i+1;j<n;j++)
if(a[min]>a[j])
min=j;
if(min!=i){
int t=a[min];
a[min]=a[i];
a[i]=t;
}
}
for(int i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
https://ideone.com/AGJDPS
NB: To see intermediate results in an online IDE, why not add printf calls inside the loop? Of course, for larger code projects you'd better use a locally installed IDE with all the debugging features, and step through the code.
I' did this program what suppose save pairs of string ,int on one vector and print the strings of the maximum number on vector
but when i try to find this strings don't appears nothing so I try print all values of int's on vector and although was finding the maximum of 10 all values in the vector was printing as 0. Someone can explain was it occurred and how I can access the values , please.
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef vector<pair<string,int>> vsi;
bool paircmp(const pair<string,int>& firste,const pair<string,int>& seconde );
int main(int argc, char const *argv[]) {
vsi v(10);
string s;
int n,t;
cin>>t;
for (size_t i = 0;i < t;i++) {
for (size_t j = 0; j < 10; j++) {
cin>>s>>n;
v.push_back(make_pair(s,n));
}
sort(v.begin(),v.end(),paircmp);
int ma=v[v.size()-1].second;
cout<<ma<<endl;
for (size_t j = 0; j < 10; j++) {
cout << v.at(j).second <<endl;
if(v[j].second == ma)
cout<<v[j].first<<endl;
}
}
return 0;
}
bool paircmp(const pair<string,int>& firste,const pair<string,int>& seconde ){
return firste.second < seconde.second;
}
This line
vsi v(10);
creates you a std::vector filled with 10 default-constructed std::pair<std::string, int>s. That is, an empty string and zero.
You then push_back other values to your vector but they happen to be sorted after those ten initial elements, probably because they all have positive ints in them.
Therefore, printing the first member of the first ten elements prints ten empty strings.
This is all I can guess from what you have provided. I don't know what you are trying to accomplish with this code.
Try something like
for (const auto& item : v)
{
std::cout << "{ first: '" << item.first << "', "
<< "second: " << item.second << " }\n";
}
to print all elements of the vector v.