Related
I am working on code where I have nested 'for loops' which is often used for calculation of matrix.
Below is example.
for( j = 0; j < col; j++ )
{
for( i = 0; i < rows; i++ )
{
float temp_var = *(mat1 + ( i * Col) + j );
for( k = 0; k < rows; k++ )
{
if( k != i )
{
if( *(mat1 + ( k * col) + j ) == temp_var )
{
count++;
}
}
}
*(mat1 + ( i * col) + j ) = count;
}
}
for( j = 0; j < rows; j++ )
{
count = 0;
for( i = 0; i < col; i++ )
{
float temp_var = *(mat1+ ( j * col) + i );
for( k = 0; k< col; k++)
{
if( k != i)
{
if( *(mat1 + ( j * col) + k ) == temp_var )
{
count = count + 2;
}
}
}
*(mat1 + ( j * col) + i ) = count;
}
}
One way would be splitting them into different functions. But I would like to if I can optimize in more efficient manner and also to improve readability .
Some minor ways in which you can make your code more readable and concise:
count += 2 (instead of count = count + 2)
As (mat1+ ( j * col) + i ) is reused multiple times, you can store it in a variable like
auto myVar = (mat1+ ( j * col) + i ); this would make you code more readable.
Bonus: Whenever you don't need indexes in for loop, for(auto x: arr) can be a wise option.
I have done a stochastic NN but the variation is too big so i want to go for a batch one but, as far as I've tried, I can't get acceptable results, I can't understand when I have done the forward how can I combine data to update weights.
There is an input layer, one hidden and one output.
For now I have tried to add all the deltas together and to average them but with poor results.
This is my stochastic version for one epoch.
for( np = 1 ; np <= numPattern ; np++) { /* repeat for all the training patterns */
p = ranpat[np];
for( j = 1 ; j <= numHid ; j++ ) { /* compute hidden unit activations */
SumH[p][j] = WeightIH[0][j] ;
for( i = 1 ; i <= numIn ; i++ ) {
SumH[p][j] += allData[p].in[i] * WeightIH[i][j] ;
}
Hidden[p][j] = 1.0/(1.0 + exp(-SumH[p][j])) ;
}
for( k = 1 ; k <= numOut ; k++ ) { /* compute output unit activations and errors */
SumO[p][k] = WeightHO[0][k] ;
for( j = 1 ; j <= numHid ; j++ ) {
SumO[p][k] += Hidden[p][j] * WeightHO[j][k] ;
}
Output[p][k] = 1.0/(1.0 + exp(-SumO[p][k])) ; /* Sigmoidal Outputs*/
Error -= ( allData[p].out[k] * log( Output[p][k] ) + ( 1.0 - allData[p].out[k] ) * log( 1.0 - Output[p][k] ) ) ; /*Cross-Entropy Error*/
DeltaO[k] = allData[p].out[k] - Output[p][k]; /* Sigmoidal Outputs, Cross-Entropy Error */
}
for( j = 1 ; j <= numHid ; j++ ) { /* 'back-propagate' errors to hidden layer */
SumDOW[j] = 0.0 ;
for( k = 1 ; k <= numOut ; k++ ) {
SumDOW[j] += WeightHO[j][k] * DeltaO[k] ;
}
DeltaH[j] = SumDOW[j] * Hidden[p][j] * (1.0 - Hidden[p][j]) ;
}
for( j = 1 ; j <= numHid ; j++ ) { /* update weights WeightIH */
DeltaWeightIH[0][j] = eta * DeltaH[j];
WeightIH[0][j] += DeltaWeightIH[0][j] ;
for( i = 1 ; i <= numIn ; i++ ) {
DeltaWeightIH[i][j] = eta * allData[p].in[i] * DeltaH[j];
WeightIH[i][j] += DeltaWeightIH[i][j] ;
}
}
for( k = 1 ; k <= numOut ; k ++ ) { /* update weights WeightHO */
DeltaWeightHO[0][k] = eta * DeltaO[k];
WeightHO[0][k] += DeltaWeightHO[0][k] ;
for( j = 1 ; j <= numHid ; j++ ) {
DeltaWeightHO[j][k] = eta * Hidden[p][j] * DeltaO[k];
WeightHO[j][k] += DeltaWeightHO[j][k] ;
}
}
}
I want to create a program that computes the path of minimum cost between 2 elements “S” and “E”,while carrying a pizza. We have a grid (2d array) that consists of ., W, X, S and E. “X” are walls-obstacles. If we move while carrying the load, every move (L,R,U,D) costs 2. However, we can leave the pizza in a wormhole “W” and take it again from another “W” in our grid, reducing the cost of our moves to 1. Leaving the pizza and taking it again costs 1.
I have written a program, but it consumes a lot of memory and the grader doesn’t accept it. I use a priority queue to store every next situation. The array prev is 3d, because we want to store, because prev[i][j][0] means we don’t carry the pizza and prev[i][j][1], means we carry it.
Here is a simple input :
S..........XX...X.X.X..XX.
.XXXXXXX.X.XX.X.X....X....
.X.....WXX..X.X.X..X...X.X
...XXXWWWWX...X...X..X.X..
XXX...XXXXXXXXXXXXXXXXXXX.
.W..XE....................
Can anyone help?
#define MAX_ROWS 1000
#define MAX_COLUMNS 1000
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
char path[1000000] ;
char grid[MAX_ROWS][MAX_COLUMNS];
char line[MAX_COLUMNS] ;
int row_coordinate ;
int column_coordinate ;
bool loaded ;
typedef struct {
bool loaded ;
int row_coordinate ;
int column_coordinate ;
}situation ;
typedef struct {
int row_c ;
int column_c ;
char move ;
}previous ;
typedef struct coordinates{
int row ;
int column ;
}position ;
typedef struct {
int priority ;
situation *data ;
} node_t ;
typedef struct {
node_t *nodes ;
int len ;
int size ;
} heap_t ;
void push (heap_t *h , int priority , situation *data) {
if (h->len + 1 >= h->size) {
h->size = h->size ? h->size * 2 : 4;
h->nodes = (node_t *)realloc(h->nodes, h->size * sizeof (node_t));
}
int i = h->len + 1;
int j = i / 2;
while (i > 1 && h->nodes[j].priority > priority) {
h->nodes[i] = h->nodes[j];
i = j;
j = j / 2;
}
h->nodes[i].priority = priority;
h->nodes[i].data = data ;
h->len++;
}
situation *pop (heap_t *h) {
int i, j, k;
if (!h->len) {
return NULL;
}
situation *data = h->nodes[1].data;
h->nodes[1] = h->nodes[h->len];
h->len--;
i = 1;
while (1) {
k = i;
j = 2 * i;
if (j <= h->len && h->nodes[j].priority < h->nodes[k].priority) {
k = j;
}
if (j + 1 <= h->len && h->nodes[j + 1].priority < h->nodes[k].priority) {
k = j + 1;
}
if (k == i) {
break;
}
h->nodes[i] = h->nodes[k];
i = k;
}
h->nodes[i] = h->nodes[h->len + 1];
return data;
}
int isEmpty(heap_t *ptr_heap){
if (ptr_heap == NULL){
return FALSE ;
}
if (ptr_heap->size == 0){
return TRUE ;
} else {
return FALSE ;
}
}
previous prev[1000][1000][2] ;
int main()
{
FILE *FilePtr;
int columns=0,rows=0,priority,i,j,k,cost,min_cost,length_of_path,l,c;
position start ;
FilePtr = fopen("text2.txt" , "r");
if(FilePtr == NULL)
{
printf("Can't open file");
exit(EXIT_FAILURE);
}
while (fgets(line,sizeof line,FilePtr)!= NULL)
{
for (columns=0; columns<(strlen(line)-1); ++columns){
grid[rows][columns]=line[columns] ;
//initialization of start
if (grid[rows][columns] == 'S'){
start.row=rows ;
start.column=columns ;
}
}
grid[rows][columns]='\0' ;
++rows ;
}
//until here cool !
//initialization of init(initial situation)
situation *init = &(situation){ .loaded = true , .row_coordinate = start.row , .column_coordinate = start.column} ;
heap_t *h = (heap_t *)calloc(1,sizeof (heap_t)) ;
//push init in queue with priority = 0
priority = 0 ;
push(h,priority,init) ;
//initialize array of situations prev
//prev is a 3d array !
for (i=0; i<rows; ++i){
for (j=0; j<columns; ++j)
for (k=0; k < 2; ++k) {
prev[i][j][k].move = 'O' ;
prev[i][j][k].row_c = -1 ;
prev[i][j][k].column_c = -1 ;
}
}
//prev[init] = "none"
prev[0][0][1].move = 'n' ;
//initializations
min_cost = 0 ;
cost = 0 ;
i = 0 ;
j = 0 ;
k = 1 ;
c = 0 ;
l = 0 ;
length_of_path = 0 ;
//initialize the current situation
situation *current = &(situation){ .loaded = true , .row_coordinate = 0 , .column_coordinate = 0 } ;
while(!isEmpty(h)){
current = pop(h) ;
i = current->row_coordinate ;
j = current->column_coordinate ;
if(current->loaded == true){k = 1 ;}
else {k = 0 ;}
//printf("%d\n",i) ;
//printf("%d\n",j) ;
//printf("%d\n",k) ;
//exit(1) ;
//until here cool
if ((grid[i][j] == 'E' )&&(k == 1)) {
min_cost = cost ;
printf("%d\n" , min_cost) ;
while (prev[i][j][k].move != 'n'){//as long as we are not in the initial situation
printf("%d\n" , min_cost) ;
//exit(1) ;
path[length_of_path] = prev[i][j][k].move ;//we store the path in reverse order
length_of_path++ ;
if (prev[i][j][k].move == 'L'){j-- ; }
else if (prev[i][j][k].move == 'R'){j++ ;}
else if(prev[i][j][k].move == 'U'){i-- ;}
else if(prev[i][j][k].move == 'D'){i++ ;}
else continue ;//we are in warmhole no change of coordinates
}
//exit(1) ;
for (l=length_of_path; l>=0; l--){//prints the solution/best path
printf("%d\n" , min_cost) ;
printf("%c" , path[l]) ;
}
//exit(1) ;
}
else {
//m = "L"
j-- ;
//we find the next situation
if (j>=0){
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){ // if next doesn't exist in prev
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'L' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost + c ;
push(h,priority,next) ;
}
j++ ; // go back to the begging
//m = "R"
j++ ;
//we find the next situation
if (j<columns){
situation *next = &(situation){ .loaded = true , .row_coordinate = 0 , .column_coordinate = 0} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'R' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost + c ;
push(h,priority,next) ;
}
j-- ;
//m = "U"
i-- ;
//we find the next situation
if (i>=0){
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'U' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost + c ;
push(h,priority,next) ;
}
i++ ;
//m = "D"
i++ ;
//we find the next situation
if (i<rows){
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
if (prev[i][j][k].row_c == -1){
//prev[next] = (i,j,m) ;
prev[i][j][k].row_c = i ;
prev[i][j][k].column_c = j ;
prev[i][j][k].move = 'D' ;
if (k == 0) {c = 1 ;}//lakis doesn't carry the pizza
else {c = 2 ;}
}
cost = cost + c ;
priority = cost ;
push(h,priority,next) ;
}
i--;
// if m = "W"
c = 1 ; //lakis takes or leaves the pizza
cost = cost + c ;
priority = cost ;
situation *next = &(situation){ .loaded = true , .row_coordinate = i , .column_coordinate = j} ;
if (k == 0) {next->loaded = false ;}
push(h,priority,next) ;
}
}
fclose(FilePtr) ;
return 0;
}
Since i'm new in the programming world ,i'm facing little problem while writting program for this pattern .I tried many times but the result is not what i wanted ?
The pattern is :
1
23
456
78910
What i have written is :-
#include<stdio.h>
#include<conio.h>
void main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch() ;
}
#include <stdio.h>
int main()
{
printf("1\n23\n456\n78910\n");
return 0;
}
produces the output you desire
You need to print a newline after the inner loop:
#include<stdio.h>
#include<conio.h>
int main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
printf("\n");
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch();
return(0);
}
There is another example:
int main()
{
int i, j, num = 1, line = 4;
for(i = 1; i <= line ; i++)
{
for(j = 0; j < i; j++)
{
printf("%d", num);
num++;
}
printf("\n");
}
return 0;
}
With single loop:
# include<stdio.h>
# define LIMIT 100
int main(){
int i, prev=0, next=0, diff=1;
for(i=1;i<LIMIT;i++){
printf("%d", i);next++;
if(diff == next-prev){
printf("\n");
diff++;prev = next = 0;
}
}
}
I've heard about backtracking and I've searched a little bit .I thought I got the idea and wrote this piece of code to solve sudoku , but it seems to give wrong solutions (for example repeated numbers in a row) , what exactly i am doing wrong here ?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 9
bool not_in_row(int temp , int i , int grid[N][N]){
int f ;
for(f = 0 ; f < N ; f++)
if(grid[i][f] == temp)
return false ;
return true ;
}
bool not_in_column(int temp , int j , int grid[N][N]){
int f ;
for(f = 0 ; f < N ; f++)
if(grid[f][j] == temp)
return false ;
return true ;
}
bool not_in_sq(int i , int j , int grid[N][N] , int temp){
int k , t ;
int s = i - (i % 3) + 3, v = j - (j % 3) + 3;
for(k = i - (i % 3) ; i < s ; i++)
for(t = j - (j % 3) ; j < v ; j++)
if(grid[k][t] == temp)
return false ;
return true ;
}
bool sudoku_Solver(int grid[N][N] , int position){
if(position == 81)
return true ;
int i = position / 9 , j = position % 9 ;
if(grid[i][j] != 0)
return sudoku_Solver(grid , position + 1) ;
else{
int temp ;
for(temp = 1 ; temp <= N ; temp++){
if(not_in_row(temp , i , grid) && not_in_column(temp , j , grid) && not_in_sq(i , j , grid , temp))
{
grid[i][j] = temp ;
if(sudoku_Solver(grid , position + 1))
return true ;
}
}
}
grid[i][j] = 0 ;
return false ;
}
int main(int argc, char *argv[]) {
int i , j ;
int grid[9][9] = {{0,1,0,0,4,0,0,0,0}
,{6,0,0,0,0,0,0,1,8}
,{0,0,0,1,0,9,0,3,2}
,{2,0,5,0,0,3,8,0,0}
,{0,0,0,0,0,0,0,0,0}
,{0,0,4,7,0,0,1,0,5}
,{8,6,0,2,0,5,0,0,0}
,{4,2,0,0,0,0,0,0,9}
,{0,0,0,0,3,0,0,7,0}
} ;
sudoku_Solver(grid , 0) ;
for(i = 0 ; i < 9 ; i++){
for(j = 0 ; j < 9 ; j++){
printf("%d " , grid[i][j]) ;
}
printf("\n") ;
}
return 0;
}
Your loops in not_in_sq are wrong:
for(k = i - (i % 3) ; i < s ; i++)
for(t = j - (j % 3) ; j < v ; j++)
Look closely at i < s, i++, j < v, and j++. They are obviously incorrect. Here they are fixed:
for(k = i - (i % 3) ; k < s ; k++)
for(t = j - (j % 3) ; t < v ; t++)