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 4 years ago.
Improve this question
I have to store the matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
in a variable called "a".I am given the following code block
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
.................
which I have to finish.I think there should be some connection between the elements of the matrix,but I can't see any.
EDIT:I must not use any additional variables.
You have an array of 5 times 5 integral values; i obviously stands for a row index, whereas j indicates a column index. The respective value in each cell then is (i-1)*5 + j.
But, probably a pitfall introduced by your teacher, be aware that array indizes in c++ start from 0, not 1. And that's why I'm giving more hints than usual for a question without any attempt of solving it:
int arr[5][5];
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
val = // enter the expression here
rowIndex = i-1;
columnIndex = j-1;
// insert the one missing statement here :-)
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am taking a course on edx.org Introduction to C++ by Microsoft. I get unwanted output when looping through a single dimensional array. The code is below.
<#include <iostream>
int main() {
int arrayName[10] = { 1,2,3,4,5,6,7,8,9,10 };
for (int i = 1; arrayName[i] <= 20; i++) {
std::cout << i << std::endl;
}
The output of this is:
1
2
3
4
5
6
7
8
9
10
11
Where does the 11 come from? And, if I make i=0, it also prints a 0. How does it print more than 10? And, when I try to change arrayName[10] to arrayName[9], I get a compiler error that there are too many initialized values:
int arrayName[10] = { 1,2,3,4,5,6,7,8,9,10 };
do {
std::cout << i << std::endl;
i++;
} while (arrayName[i] < 5);
The output is:
12
13
14
15
16
17
18
That do-while loop outputs 7 integers that I did not specify to be included in the arrayName[] array.
I don't know what I am doing wrong or what I am not understanding.
Please help. Thank you!
First, note that arrays in c++ start at index 0. So in int arrayName[3] = {10, 42, 88}; then arrayName[1] is 42, not 10. That means the last element in this array is int arrayName[2]. There is no element at index 3.
Your array only contains 10 elements (indices 0 to 9). The standard does not specify what happens when you access an element past the end of an array, anything can happen. In your case, arrayName[10] and arrayName[11] happens to give you something less than or equal to 20, and then arrayName[12] gave you something greater than 20, ending the loop. If you try it on another computer, or even at a different time, the results will vary. It might also crash (this is the best case scenario).
See this answer for more information on undefined behavior.
I finally found this: Correct way of loop through the C++ arrays, answer by https://stackoverflow.com/users/1619294/mark-garcia.
Changed my code to:
std::cout << "Looping through arrayName3 with std::array and letting the compiler determine how many objects to print:" << std::endl;
// Need to #include <array>
std::array<int, 10> arrayName3 = { 1,2,3,4,5,6,7,8,9,10 };
for (const auto& i : arrayName3) // Range-for
{
std::cout << i << std::endl;
}
The output was what I wanted:
1
2
3
4
5
6
7
8
9
10
This let's the compiler know it is deciding what to output. It would be great to know how to change this to control how many indices to loop through.
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 2 years ago.
Improve this question
My initial question was a bit vague, so here's the edited question.
The 3n+1 uVa problem is based on the Collatz Conjecture.
Consider any positive integer 'n'. If it is even, divide it by 2. If it is odd, multiply it by 3 and add 1. After 'x' such repeated operations, you will get 1. This has been proven for very large numbers by supercomputers.
UVa Online Judge is an online automated judge for programming problems hosted by University of Valladolid.
Here's a link to the problem statement:
uVa 3n+1
Please read the problem statement before looking at my code!
The uVa online judge runs your code against certain test-cases and tells you if your solution was right or wrong. It does not tell you why it failed (if your solution was wrong).
Here is the link again if you missed the first one:
The link to the problem description
I don't understand what I am missing out or skipping (since I am getting Wrong Answer) in my code logic. I have tried lots of test cases, and they seem to be working fine. I know the logic can be compressed to a large extent, but I just need to figure out where the flaw is for now.
Here is my code. I know bits/stdc++ shouldn't be used, but currently, it's not affecting my code. Please review it if you can and help me out.
#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
using namespace std;
int main()
{
unsigned long int num;
int i,j,tempi, tempj,temp;
//Scanning until inputs don't stop
while(scanf("%d %d",&i,&j)!=EOF)
{
//Boolean variable to set if i is greater
bool iwasmore=false;
//Boolean variable for equal numbers
bool equalnums=false;
int cycles=1, maxcycles=0;
if(i>j)
{
//swapping for the for loop to follow
temp=i; i=j; j=temp;
iwasmore=true;
}
if(i==j)
{
equalnums=true;
}
tempi=i; tempj=j;
//Taking each number in the given range and running it in the algorithm.
//The maxcycles variable will have the value of the maximum number of cycles
//that one of the numbers in the range took (at the end of the for loop).
for(num=i;num<=j;num=(++tempi))
{
if(cycles>maxcycles)
{
maxcycles=cycles;
}
cycles=1;
//The actual algorithm
while(num!=1)
{
if(num%2==1)
{
num = (3*num)+1;
cycles++;
}
else
{
num=(num/2);
cycles++;
}
}
if(equalnums==true)
{
maxcycles=cycles;
equalnums=false;
}
//Resetting num
num=0;
}
if(!iwasmore)
cout<<i<<" "<<j<<" "<<maxcycles<<endl;
else
cout<<j<<" "<<i<<" "<<maxcycles<<endl;
}
return 0;
}
Here is the output I am getting for the following input:
Input:
1 1
10 1
210 201
113383 113383
999999 1
Output:
1 1 1
10 1 20
210 201 89
113383 113383 248
999999 1 525
Another testcase:
Input:
1 5
10 8
210 202
113383 113383
999999 999989
Output:
1 5 8
10 8 20
210 202 89
113383 113383 248
999999 999989 259
The cycle length of the last number checked is never compared against maximum.
For Input:
8 9
9 9
The program outputs
8 9 4
9 9 20
But the correct answer is
8 9 20
9 9 20
Put
if (cycles > maxcycles) {
maxcycles = cycles;
}
at the end of the for-loop, instead of at the beginning.
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 7 years ago.
Improve this question
This is the link of the problem.
https://projecteuler.net/problem=8
below is my code.
#include <stdio.h>
int main() {
long i,sum;
long temp = 0;
long arr[1000] = {
// Increasingly large number is ommitted//
// I just add ',' between each numbers//};
for(i=0; i<988; i++){
sum = arr[i]*arr[i+1]*arr[i+2]*arr[i+3]*arr[i+4]*arr[i+5]*arr[i+6]
*arr[i+7]*arr[i+8]*arr[i+9]*arr[i+10]*arr[i+11]*arr[i+12];
if(temp<sum){
temp = sum;
}
}
printf("%ld",temp);
return 0;
}
so I got 2091059712 which seems kind of reasonable answer.
The real problem here is, that you did not account for the size of the product. An integer is 10 digits max (2,147,483,647). So this or something alike might happen:
sum = 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9;
This gives: 2,541,865,828,329 which overflows your integer leading to undefined behaviour.
Use a larger integer type or take a different approach.
That's a brute force solution that will work fine for this size of problem.
Potential improvements:
Split the array on "0", and only test the substrings that are longer than the desired length.
Print out the numbers that ended up being the best substring. That way you can test that it actually is present in the original and the multiplication is done correctly.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.
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
Say I have a table that goes clockwise like this:
2
1 3
0
This works fine in a 4 person game, but in a 3 person game, the server does not see a 4th chair. Thus, I must map 3 players, one of whom may sit at seat [3] to an array of 3 [2] elements.
So if players were sitting at 2,3,0 then 2 maps to 0, 3 maps to 1 and 0 maps to 2. If the seating was 1, 3, 0 it would map 1 to 0, 3 to 1, and 0 to 2.
What would be a good algorithm to do this if I wanted a std map for this?
Assuming you have an ordered structure of the seats, and making a few assumptions because the details of your implementation are not clear:
std::map<int, int> clockwisePositionMap;
int counter = 0;
for (Seat s : seatList) {
if(s.isOccupied()) {
clockwisePositionMap.insert( pair<int, int>(seat.getNumber(), counter));
counter++;
}
}
Note that this does not match either of your examples; it always gives 0 first precedence, but you can change the for loop as necessary if that is a problem.