Here is the task:
Project U6: Array Walker
Write a program that computes a ”walk” (of a cute robot) across a 10x10 array. The
array contains characters (all initially ’.’). The user can enter four different directions
north, east, south, west, by entering the characters n,e,s,w to control the walk. If the user
enters x the program exits. After each direction-command the array is printed with the
walk being display by capital letter starting with A to Z. We use A for the starting point,
with 25 remaining letters to denote the steps. After having used letter Z to denote a step,
we wrap around and start again with A. To make the walk more interesting, the user is
not allowed to revisit a location. In this case the program does not print the array, but
prints ”You cannot go there!” - Yes, you can trap yourself.
Starting in the upper left corner (position 0,0):
Thats what i have so far:
#include <stdio.h>
#include <conio.h>
#define M 26
#define N 10
#define K 10
int main()
{
char A[N][K],direction;
char let[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int i=0,j=0,m=0;
for(i=0; i<N; ++i)
for(j=0; j<K; ++j)
A[i][j]='.';
for(i=0; i<N; ++i)
{ A[0][0]=let[0];
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");
}
getch();
scanf("%c",&direction);
for(m=1;m<26;m++){
if (direction=='E')
for(i=0; i<N; ++i){ //i don't think that the way that im thinking here is rigth
A[0][0]=let[0];
m=i+1;
A[m][j++]=let[m];
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");}
return direction;
}
return 0;
}
I know that i'm not even close to solution, but i rly want to know how to make it.
I can't understand how to change '.' on a following letter, and remember all that positions.
I'm not going to give you the entire code ,but just give you some tips and hints.
First of all,call
A[0][0]=let[0];
once at the start of main.Then,use create a function named dispgrid or something like that with
for(i=0; i<N; ++i)
{
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");
}
In its body. Remember that
char A[N][K];
Should be declared as global because other functions also will need it. You will be needing a pointer to iterate through the letter array. So declare
char *ptr=let;
You will also need two variables which denotes the current x and y coordinates of the robot. So declare
int x=0,y=0; //starting at position (0,0)
I imagine the grid with the top-left corner having coodinates (0,0) and bottom-right corner having coordinates (-9,-9). Then,create a loop which terminates when direction is x. Scan user input and store it in direction. Then,check if it is n . If it is,check if y is 0 and if a[x][y-1] is not . . If it is,then print "You cannot go there" and continue the loop. Else,do y-- and then if ptr is Z,ptr=let and [x][y-1]=ptr;else,ptr++; and then, a[x][y-1]=ptr;. Continue the loop after that.
Now,Implement the above idea,changing the respective variables when neccesary when direction is e,w and s.
i did it!
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define M 26
#define N 10
#define K 10
int main()
{
char A[N][K],direction;
char let[M] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char start='A';
int x=0,y=0;
int i=0,j=0,m,k;
for(i=0; i<N; ++i){
for(j=0; j<K; ++j)
A[i][j]='.';}
for(i=0; i<N; ++i){
A[0][0]=let[0];
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");
}
k=0;
A[x][y]=let[k];
if (direction == 's' && x==9 || direction=='e' && y==9 || direction=='w' && y==0 || direction=='n' && x==0){
printf("You cannot go there!\n");}
do{
if (direction =='x'){break;}
if (direction!='\n'){
printf("Enter direction command: ");}
direction=getchar();
if (direction=='s' && A[x+1][y]!='.'){
printf("You cannot go there!\n");
}else if(direction=='s') {
x++;
k++;
A[x][y]=let[k];
for(i=0; i<N; ++i){
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");}
}
if (direction=='e' && A[x][y+1]!='.'){
printf("You cannot go there!\n");
}else if (direction=='e'){
y++;
k++;
A[x][y]=let[k];
for(i=0; i<N; ++i){
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");}
}
if (direction=='w' && A[x][y-1]!='.'){
printf("You cannot go there!\n");
}else if (direction=='w'){
y--;
k++;
A[x][y]=let[k];
for(i=0; i<N; ++i){
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");}
}
if (direction=='n' && A[x-1][y]!='.'){
printf("You cannot go there!\n");
}else if (direction=='n'){
x--;
k++;
A[x][y]=let[k];
for(i=0; i<N; ++i){
for(j=0; j<K; ++j)
printf("%c",A[i][j]);
printf("\n");}
}
} while (k!=26);
}
Related
Firstly, a number N has to be input, and the matrix is of NxN dimensions then. The diagonal of the matrix has to be all 0, the elements of the matrix above the diagonal have to be from 1 to N diagonally, and the elements under the diagonal need to be filled from -1 to -N also diagonally. It should be something like this (if N=5):
But the problem that I have is that I print it out like this:
and I don't know how to fix it.
This is the code that I have:
`#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d",&N);
int k=0;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(i==j){
matrix[i][j]=0;
}
else if(j>i && i!=j){
for(k=0; k<N; k++){
matrix[k-1][j]=k;
}
}
else if(j<i && i!=j){
for(k=0; k<N; k++){
matrix[i][k-1]=-k;
}
}
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}`
I would really appreciate the help.
Here is you code modified, notice that 3 inner loops are removed with only one line.
second, you ask for the number N, however due to statically initialisation to maximum 50, you should as well verify that it is not. otherwise segmentation fault will happen.
or if you want to allow N >50 then better to do dynamic allocation on matrix.
#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d", &N);
if (N > 50){
printf("N should be smaller than 50, \n");
N = 50;
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
matrix[i][j]= j - i;
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}
I am solving this problem
https://www.acmicpc.net/problem/1238#
You can change the language by clicking the button
The idea I came up with is to find the sum of the shortest distance from the Kth to the second and the second to the Kth
so here is my whole source code
#include <stdio.h>
#define INF 999999
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;
int ans = 0;
int n,m,x;
int d[1001][1001];
void Floyd_Warshal(){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(i==j) d[i][j]=0;
}
}
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
void solve(){
for(int i=1; i<=n; i++){
if(i==2) continue;
if(d[i][2] + d[2][i]>ans) ans = d[i][2] + d[2][i];
//printf("%d = %d+%d \n",ans,d[i][2],d[2][i]);
}
}
int main(){
scanf("%d %d %d",&n,&m,&x);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++) d[i][j] = INF;
}
for(int i=0; i<m; i++){
int u,v,t;
scanf("%d %d %d",&u,&v,&t);
d[u][v] = t;
}
Floyd_Warshal();
solve();
printf("%d\n",ans);
return 0;
}
I think Floyd_warshal() function is fine.
But, I guess I take a wrong approach(The idea suggested above) to solve the problem so I just wanna ask that my idea is right approach to solve the problem or not.
The complexity of the Floyd-Warshall algorithm is O(n3). It will be TL.
The solution to this task is to doing SSSP (single source shortest path). It can do effectively with Dijkstra's Algorithm. You can do Dijkstra's Algorithm on farm X then doing it again with edges reversed. It has O(m logn) complexity if you write it with priority queues or set. Google for this, there are many information, for example 1, 2.
After this your idea is right, you should take maximum of sum D[i][X] + D[i][X] for each farm i. Though you have written 2 instead of x, but it is probably a typo, or to test code.
I am trying to learn C and I am trying to write a piece of code which does the following:
Take user input of a natural number n
Take user input of n elements and store them in the array x
Delete all negative numbers from the array x
Print the new array, with the length n - number of deleted elements
Here is my code:
#include <stdio.h>
int main(void)
{
int n, i, count=0;
double x[1000];
scanf("%d", &n);
for (i=0; i<n; i++)
scanf("%lg", &x[i]);
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
x[i-count]=x[i];
};
n -= count;
for (i=0; i<n; i++)
printf("%d: %g\n", i, x[i]);
return 0;
}
I have been told that I should replace my second for loop with the following code:
int j=0
...
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
if (i > j)
x[j] = x[i];
j++;
};
Could someone please explain why is the latter code better?
If i==j, then you're assigning an element to itself: not wrong, but a (small) waste of effort.
If you really want to improve this, avoid putting the negative values in the array in the first place.
This is my initial board. The first problem I'm having is that when the user enters the board size, it always prints the same 10 by 10 rather than what the user asked for. Secondly, I have a function below which is supposed to check each cell and convert the cells which match the condition to a whitespace or dash. I'm having trouble printing the board. Can anyone please tell me where I'm going wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
int createBoard();
int main(int argc, char *argv[])
{
createBoard();
}
int createBoard()
{
char myArray[MAX][MAX];
char letter[3] = {'a', 'b', 'c'};
int i,j,row,col;
printf("Please enter your grid size: ");
scanf("%d %d", &row, &col);
if(row < 10 && col < 10){
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
myArray[i][j] = letter[rand()%3];
}
}
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
printf("%c ", myArray[i][j]);
}
printf("\n");
}
}
else{
printf("Board is too big\n");
createBoard();
}
int var;
var = move(myArray);
}
//to check each cell
int newBoard(char myArray[MAX][MAX])
{
int i,j;
for(i=0; i < MAX; i++){
for(j=0; j< MAX; j++){
if(myArray[i][j] == 'c' && myArray[i+1][j] == 'c'){
myArray[i][j] == ' ';
myArray[i+1][j] == ' ';
}
else{
//no update
}
}
}
}
For 1st problem change your for loops
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
...
}
}
to use row and col variables
for(i=0; i < row; i++){
for(j=0; j < col; j++){
...
}
}
first problem solotion :
because in your for loop you go through MAX
change it to :
for(i=0; i < row; i++){
for(j=0; j < col; j++){
myArray[i][j] = letter[rand()%3];
}
}
second problem solution :
first problem with check function is that you should pass my Array by reference not by value
change it by adding'&' sign
second problem is you have to say if(myArray[i][j] == 'c' && myArray[i+1][j] == 'c')
this cause problem when i is equal to MAX-1 (last for step) and myArray[i+1][j] does not exist !!
because the size of array is [MAX][MAX] and when i = MAX-1 , you size would be [MAX+1][MAX]
I'm creating a sudoku solver in C and having trouble obtaining user input. The code that I've written doesn't input the data into the game board, but if I change Game_Buffer[counter] to Game_Buffer[i] it inputs the data but only 9 characters. I am aware of why. I just wanted to see if their were problems in other areas.
My primary question is: Why is the method I'm using not placing the user input data into the game board array?
#include <stdio.h>
#include <string.h>
#define CELL 81
int main()
{
// Banner
printf("\t\t\t\tSudoku Solver\n");
printf("\t\t\t***************************\n");
//initialize variables
char Game_Board[9][9];
int i,j;
char Game_Buffer[CELL];
int counter = 0;
printf("Please enter the numbers of the board * denotes a blank space\n");
fgets(Game_Buffer,CELL,stdin);
for(i=0;i<strlen(Game_Buffer);i++)
printf("%c", Game_Buffer[i]);
while(counter < 81)
{
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
printf("%d\n", counter);
printf("\t\t\t\t The Board\n");
for( i=0; i<9; i++)
for( j=0; j<9; j++)
{
if( j % 3 == 0)
printf("|");
printf("%c", Game_Board[i][j]);
if(j==8)
printf("|\n");
}
return 0;
}
You probably should use brackets at first place.
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
}
Add all missing brackets and check if your issue still exists.
The counter++ executes after the loop. I have idented the code to show what I mean..
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
You are updating all cells with the same value.