srand(time(NULL)) not giving strings anymore [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
As a beginner in programming I can not find why srand would just not change the string anymore.
#include <stdlib.h>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int v[100], i, p, n,
srand(time(NULL));
cout<<"Numbers of numbers: "; cin>>n; cout<<endl;
for(i=0; i<=n-1; i++)
v[i]=rand()%100;
cout<<"Numbers: "<<endl;
for(i=0; i<=n-1; i++)
cout<<v[i]<<" ";
for(i=0; i<=(n-1)/2; i++){
p=v[i];
v[i]=v[n-(i+1)];
v[n-(i+1)]=p;
}
cout<<"Reversed numbers: "<<endl;
for(i=0; i<=n-1; i++)
cout<<v[i]<<" ";
return 0;
}
Tried rebuilding it, rewriting it from 1, and such. Even if it did work perfectly before it's just one type of bug that yeah, simply won't work.
Edit: Weird because I copy pasted the beginning and it worked with that... I guess it was my mistake. Not paying enough attention T_T

If you enable more warnings you will see something like
program.cpp:11:1: warning: unused variable 'srand' [-Wunused-variable]
srand(time(NULL));
^
The reason can be seen on the previous line: It ends in a comma
int v[100], i, p, n,
// Note comma here ^
So what the code is doing is declaring a variable named srand, and not calling a function called srand.
Take this as a lesson to always build with extra warnings enabled. Enabling more warnings made this problem very obvious, but it can also give hints about cases of undefined behavior.

Related

trying to plus each element of the loop over the next element [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
i'm trying to plus each elemnt of the loop result over the next elemnt
Like if i have a loop it gives me the numbers between 1~5 so i need 1+2+3+4+5
I tried below code but it didn't work
#include <iostream>
using namespace std;
int main() {
for (int i =1; i <=50 ;i++){
cout<<i<<" ";
cout<<(i +=i)<<endl;
}
return 0;
}
The variable 'i' is sort of a "control" variable that maintains the count. You are adding 'i' to itself and not actually printing it. If you need to print characters like a '+' you need to use quotation marks, unless they are variables.
The code you probably want to use is -
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i=1; i<n; i++){
cout<<i<<"+";
}
cout<<n<<endl;
return 0;
}
Or if you wanted the sum of all the elements, the statement 'i+=1' actually adds 'i' to itself, this is not good as 'i' is your iterator, or a control variable of some sort that maintains your count. You should not operate over it. You will need another variable to hold the sum as you loop through.
You probably want this in this situation -
#include <iostream>
using namespace std;
int main() {
int sum=0;
for(int i=1; i<=5; i++){
sum+=i;
}
cout<<sum<<endl;
return 0;
}

How to count odd or even numbers between particular elements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hi everyone!!! I was writing contest and actually this is not difficult question but I stuck on it. Please help, here my code that passed only three tests and failed on fourth test. Answer must not use function and pointers. Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
long long int n;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long long int max=a[0],min=[0],ind1=0,ind2=0;
for(int i=0;i<n;i++){
if(a[i]>=max) {
max=a[i];
ind2=i;
}
if(a[i]<=min) {
min=a[i];
ind1=i;
}
}
int sum=0;
if(ind1<ind2)
for(int i=ind1+1;i<ind2;i++){
if(a[i]%2==0) sum++;
}
else if(ind1>ind2)
for(int i=ind2+1;i<ind1;i++){
if(a[i]%2==0) sum++;
}
cout<<sum;
}
It looks like you're trying to count the number of even elements between the min and max element in the array?
It may have failed the last test because you set initial min/max to garbage. In the case where a[0] is initialised to zero, and all the elements in the array are negative, the reported max will be incorrect, for example. You need to set max to the minimum integer and vice versa.

no matching function for call to [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#include <iostream>
using namespace std;
int n;
void displaysum(double mat[n][n])
{
double sum= 0;
for(int j=0;j<n;j++)
sum += mat[j][j];
cout<<"Sum of Diagnols Elements is \n"<<sum;
}
int main()
{
cout << "what are the number of rows or column in the matrix" << endl;
cin >> n;
double matrix[n][n];
for (int row = 0; row < n; row++)
{
for (int column = 0; column < n; column++)
cin >> matrix[row][column];
}
displaysum(matrix)
return 0;
}
I don't understand why I get an error for no matching function to call inXCODE. Even if I try to change the variable in my function prototype it still gives me the same error.
I don't understand why I get an error for no matching function to call inXCODE.
The basic issue is that C++ wants the second dimension to be constant at compile-time for the sake of type-checking. If you want to get around this you'll have to use pointers (AFAIK). You can make this work by changing the declaration of displaysum to
void displaysum(double **mat)
and making appropriate allocations for matrix in the original function.
If you don't like this, well, welcome to C++'s type system. In the function declaration, double mat[n][n] is seen as double (*)[n]. This actually makes sense, but why it doesn't see matrix as being of that type is because n isn't constant. You can change the call
displaysum(matrix);
to this:
displaysum(static_cast<double (*)[n]>(matrix);
and receive the curious error
static_cast from 'double (*)[n]' to 'double (*)[n]' is not allowed
(and that's not the strangest error you'll get out of the type system)

error "Abort trap: 6" comes when sorting more elements than 15 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i get a error Abort trap:6 when i am sorting more than 15 elements(k>15) in numbers[]. I am using mac os x and code blocks 12.11.
code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
void getdata(int numbers[],int & k);
void sorting(int numbers[], int & k);
int main()
{
int numbers[10];
int k;
getdata(numbers, k);
sorting(numbers,k);
for(int i=0; i<k; i++)
cout<<numbers[i]<<" ";
return 0;
}
void getdata(int numbers[],int & k)
{
cin>>k;
for(int i=0; i<k; i++)
{
cin>>numbers[i];
}
}
void sorting(int numbers[],int & k)
{
int j, temp;
for(int i=1; i<k; i++)
{
j=i;
while(numbers[j]<numbers[j-1] && j>0)
{
temp=numbers[j-1];
numbers[j-1]=numbers[j];
numbers[j]=temp;
j--;
}
}
}
Line 12:
int numbers[10];
You are reserving space for only 10 integers. Use a std::vector<int> that grows with the input instead. Neither get_data nor your sorting function will then require the k parameter. The vector holds the size.
i get a error Abort trap:6 when i am sorting more than 15
elements(k>15) in numbers[].
That's because your numbers[] only contains 10 elements:
int numbers[10];
Trying to access an array element that does not exist is undefined behaviour. Which means that the program is allowed to do everything, including crashing every now and then in various ways including the one you experience, or even producing the expected output if it coincidentally feels like it.
You have to know what you want:
Either a collection of ints whose size is fixed at compile time and which cannot ever grow or shrink? If so, use std::array<int, 10> and make sure that at most 10 numbers can be entered by the user (e.g. by exiting the program with an error message if a number greater than 10 is entered).
Or a collection of ints which can grow while the program is running? If so, use std::vector<int> and push_back elements to it as their are entered.

Hint: Missed newline ? while trying to solve the below quiz [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Problem
This is similar to the previous problem, however there will be multiple integers in the input. You have to write a computer program to read each integer and print Even if the integer is divisible by 2, else print Odd. To help further, the number of integers (T) to read will be the first input to the computer program.
Input Format:
First line of input contains count of integers: T. T>=1
After that, each line contains the integer N.
Sample Input:
2
4
5
Sample Output:
Even
Odd
Note: There should be a newline after each output. Otherwise you might end up printing EvenOdd here, which will result in wrong answer.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0) cout<<"Even"<<endl;
else cout<<"Odd"<<endl;
}
return 0;
}
You don't need neither array nor vectors for this:
int main() {
int n;
cin >> n;
while(n--) {
int number;
cin >> number;
// do something with number
}
return 0;
}
Note that this structure is the most common to solve this kind of problems
your problem will be solved if you change
for(int i =0; i <sizeof(a)/sizeof(int); i++)
to
for(int i =1; i <sizeof(a)/sizeof(int); i++)
But, how'd you know the number of elemnts for the array beforehand?
To correct this, you don't declare the array statically. Take the first input, and allocate memory for that many ints.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0)
cout<<"Even"<<endl;
else
cout<<"Odd"<<endl;
}
return 0;
}
this code which u posted is working perfectly bro :)