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 trying to write a program that sorts an array (input by user) using pointers.
This is the code I wrote:
#include <stdio.h>
void main()
{
int a[100],i,n,j,t;
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Enter array:\n");
for (i=0; i<n; i++)
{
scanf("%d",a+i);
}
for (i=0; i<(n-1); i++)
{
for (j=i+1; i<n; i++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
printf("Sorted array (ascending) is:\n");
for (i=0; i<n; i++)
{
printf("%d ",*(a+i));
}
printf("\n");
}
The compiler is not showing any errors.
Input:
5
1 5 4 2 8
Output:
1 8 4 2 5
I'm learning by myself and I got the idea that pointers are similar to arrays. I know how to do the same task with arrays but when I try here to substitute them with pointers it doesn't work.
What am I doing wrong here?
the problem is in your second loop while you are sorting:
for (i=0; i<(n-1); i++)
{
for (j=i+1; i<n; i++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
you need to change the counter names to j :
for (i=0; i<(n-1); i++)
{
for (j=i+1; j<n; j++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
this line has the problem :
for (j=i+1; j<n; j++)
I think it is because your j variable is not increased properly and wrong comparison is used (as suggested by Patrick). I think it should be as follow:
for(j=i+1; j<n; j++)
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);
}
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]
Basically i want to create a 2 dimensional array size NxN, find the maximum value in each row and replace it in the upper triangle or rather replace the elements with the maximum it for that specific row, but above the diagonal.
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 100
int main()
{
int n, i, j, temp,m;
float a[MAXIMUM][MAXIMUM], max;
printf("dimensions: ");
scanf("%d",&n);
printf("input elements\n");
for (i=0; i<n; i++){
for (j=0; j<n; j++){
scanf("%f",&a[i][j]);
}
}
max=a[0][0];
for (i=0; i<n; i++){
for (j=0; j<n; j++)
{
if(max<a[i][j]){
max=a[i][j];
}
for(m=0;m<n;m++) //the problem starts here
{
if(max>a[i][m]){
if(i+m>n-1){
a[i][m]=max;
}
}
}
}
}
for(i = 0; i < n; i++) {
printf("\n");
for(j = 0; j < n; j++) {
printf("%f ", a[i][j]);
}
}
return 0;
}
J loop finishes every times, then m loop starts so the value of j is n-1 when it is in m loop.That is a problem.
maybe like this
for (i=0; i<n; i++){
max=a[i][0];
for (j=1; j<n; j++){
if(max<a[i][j]){
max=a[i][j];
}
}
for(m=i;m<n;m++){
a[i][m]=max;
}
}