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 3 years ago.
Improve this question
I'm writing a program that multiplies matrices. And here I have got two variables "i" and "q" which at the beginning are both 0. While the loops proceed variables ("i" and "q") change their values. However after the loops are done I need "i" and "q" to come back to the value 0, so the loops can repeat themselves for different "w" and "k" . How can I do so??
int wynik[x][z]; //table that holds the result of the multiplication
int i=0;
int q=0;
int wyn=0;
for(int w=0; w<x; w++)
{
for(int k=0; k<z; k++)
{
while((i<y) && (q<v) )
{
wyn = (tab1[w][i] * tab2[q][k]) + wyn;
i++;
q++;
}
wynik[w][k] = wyn;
}
}
set those to 0 after the outer loop is done:
int wynik[x][z]; //table that holds the result of the multiplication
int i=0;
int q=0;
int wyn=0;
for(int w=0; w<x; w++)
{
for(int k=0; k<z; k++)
{
while((i<y) && (q<v) )
{
wyn = (tab1[w][i] * tab2[q][k]) + wyn;
i++;
q++;
}
wynik[w][k] = wyn;
}
//HERE
i = 0;
q = 0;
}
I feel like the most natural way according to your current design would be to change the most inner while loop into for (int i = 0, q = 0; (i < y) && (q < v); i++, q++). This lets for loop manage modification of i and q and both are inside the scope of the inner for loop since they are not needed anywhere else.
I would rethink the design. It sounds like a great example to study some algorithms.
Just dropping this here ;)
https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm
Related
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
I am trying to count the number of odd values in the elements of the array. Is there a way to perform the below operation without declaring a variable inside the for loop? I am aware that the variable declared inside the loop cannot be accessed outside the loop and I want to know if there is a way that the following loop is performed and the value of oddValueCountKS could be accessed outside of the loop.
int arr[3] = {1004, -237890, 30022};
for (int i = 0; i < 3; i++) {
int oddValueCountKS = 0;
while (arr[i] != 0) {
if (arr[i] % 2) {
oddValueCountKS++;
}
arr[i] /= 10;
}
}
int arr[3] = {1004,-237890,30022};
int oddValueCountKS[3] = {0};
for (int i = 0; i < 3; i++) {
while (arr[i] != 0) {
if (arr[i] % 2) {
oddValueCountKS[i]++;
}
arr[i] /= 10;
}
}
Declare it outside the loop
int oddValueCountKS;
for (int i = 0; i < 3; i++) {
oddValueCountKS = 0;
//the rest of your code
}
this way you will be able to access it outside
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
for (int j=1; j<path.size(); j++) {
//do something
for (int z=0; z<path.size(); z++) {
//do something
}
break;
}
The inner loop can loop from 0 until it hits the end of the path size, but the outer loop just keeps giving me j=1 instead of doing the increment like inter loop does.
How can both loop do the increment at same time? I was trying to delete the break; but z loop through path.size()'s times.
The inner loop executes completely for every iteration of the outer loop, that's why you see z going looping path.size() times. After this your loop stops due to break so it won't iterate a second time over outer loop. So even if condition of outer loop is still true it won't even check it since you're breaking out of your loop.
Try running this test program with and without the break
for (int j=1; j<5; j++) {
printf("j=%d\n",j);
for (int z=0; z<5; z++) {
printf("z=%d\n",z);
}
break; // next time try to remove this break
}
and you might get enlightened.
If you want j and z to be counted up in parallel the you might want something like this
for (int j=1, z= 1; j<5; j++, z++) {
printf("j=%d\n",j);
printf("z=%d\n",z);
}
for(int j = 1 ; j < path.size() ; j++){
do something
for (int z = 0 ; z < j ; z++){
do something
}
I think that's what you're looking for
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I've got a 2d array airport[30][5] which prints out like this:
Departure, Destination, Airline, Price, Duration
I want the user to enter in their own departure and destination (stored in variables x and y), and then I will search through the array to see if the first two elements of each row matches what the user has input. If they both match, I want to print out all the rows where they both match.
So far I have a simple loop but it prints out all the lines in the array:
for(int i = 0; i < 5; i++) {
for(int j=0; j < 30; j++) {
if(airport[i][0] == x && airport[i][1] == y) {
cout << Line(s) from array
}
}
}
something like this?
for(int j=0; j < 30; j++) {
if(airport[j][0] == x && airport[j][1] == y) {
for (int i=0; i < 5; i++) {
cout << airport[j][i] << " ";
}
cout << endl;
}
}
You need only one loop
for ( int i = 0; i < 30; i++) {
if ( airport[i][0] == x && airport[i][1] == y) {
// cout << Current Line from array
}
}
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
this program creates random number array where elements wit arguments 1,3,5,7...,19 are negative and this program should find biggest negative element but when l test program it writes some random number (6784345 instead of array element) can you help me find mistake ?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void najneg(int *pa,int *nn)
{
nn=0;
for(int i=0;i<20;i++)
{
if((pa+i)<nn) nn=(pa+i);
}
}
int main()
{
int a[20],nn,i;
srand(time(0));
for(i=0;i<20;i++)
{
if(i%2==0) a[i]=rand()%(61);
else
a[i]=(rand()%(61))*(-1);
}
printf("Formirani niz je:\n");
for(int i=0;i<20;i++)
{
printf("\ "); printf("%d",a[i]);
}
najneg(a,&nn);
printf("\n\nNajveci negativni clan niza je:%d\n",nn);
return 0;
}
In this code, …
void najneg(int *pa,int *nn)
{
nn=0;
for(int i=0;i<20;i++)
{
if((pa+i)<nn) nn=(pa+i);
}
}
you forgot to dereference the pointers,
void najneg(int *pa,int *nn)
{
*nn=0;
for(int i=0;i<20;i++)
{
if(*(pa+i)<*nn) *nn=*(pa+i);
}
}
The most important fix for this function is to change its name to something readable and self-descriptive, with no arbitrary shortenings. When choosing names, think about making the calling code readable and clear. So E.g., najneg → most_negative_value_in.
Secondly, instead of logical out-argument, use the function return value.
Third, if the function doesn't need to change data, use const to let it offer a guarantee that it won't change the data.
Fourth, avoid magic numbers like 20: pass the array size as argument.
This, plus some purely cosmetic changes, yields:
int most_negative_number_in(int const* const a, int const size)
{
int n=0;
for(int i=0; i<size; ++i)
{
if(a[i]<n) { n = a[i] };
}
return n;
}
This function should be
void najneg(int *pa,int *nn)
{
*nn=0; //As you want to modify nn.
for(int i=0;i<20;i++)
{
if(pa[i]<*nn) *nn=pa[i]; //Here, you want to compare values and swap them. Not just address.
}
}
Do not try to complicate it.
In addition to what others have pointed out:
You can simplify the content of your for loops:
for(i = 0; i < 20; i++)
{
// The array slot is assigned a random value
// whether the index is positive or negative.
a[i] = rand() % 61;
// If the index is odd, change the value
// to a negative number.
if(i % 2 == 1)
{
a[i] *= -1;
}
}
In the next loop, you should combine into one printf call:
for(int i = 0; i < 20; i++)
{
printf("\ %d", a[i]);
}
Also, what is "\ " in the format specifier, a tab or a space?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
NOTE: Bad code ahead. You've been warned.
This code iterates through all elements in all 3x3 squares in a Sudoku puzzle. The way shown below is that it gets the coordinate of the upper-left-hand element for each square then iterates through each element in that square. This means that there will be a total of 4 "for" loops needed to just access the correct index, and then another "for" loop to do the correct action.
This code will work (if the retracted code were put in), but it looks very messy and is very hard to read. Is there a better way to do it that eliminates these nested "for" loops?
Thanks in advance.
void Sudoku::updateSquares(int grid[9][9], int possibleSolutions[9][9][10])
{
for (int i = 0; i < 9; i += 3)
{
for (int j = 0; j < 9; j += 3) //for every square:
{
//Other code
//...
//Other code
//updates the possibleSolutions array
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++) //for every element in every square:
{
if(grid[i+k][j+l] != 0)
continue;
for (int n = 0; n < 10; n++)
{
if(possibleSolutions[i+k][j+l][n] != 0 && numbers[n] == 0)
{
possibleSolutions[i+k][j+l][n] = 0;
possibleSolutions[i+k][j+l][0] -= 1; //reduce the size, which is held in [][][0]
}
}
}
}
}
}
}
You have implemented something called as "Exhaustive Searching" which is essentially trying out every possible combination of squares.
Have you heard about something called loop unwinding ?
--> Instead of 5 nested For loops use fewer nested loops multiple times; something like 2 nested loops .
Use a dynamic programming approach which is probably O(n^2)
Top Coder DP example.