I'm trying to make the letters of a matrix sort alphabetically and then be written out in a single string.For instance you type ten words,which are then stored in an array,and every letter has its place in the matrix then,right?But after I've written the words I want to bunch all the letters of all words together and then type all the letters out in alphabetical order.This is what I have so far:
#include <stdio.h>
#include <conio.h>
int main(void){
int i, j, k, f, n, m;
//was trying out various things,that's why I have so many useless ints up there
char word[10][15],temp;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n])
{
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
}
}
printf("Letters alphabetically sorted: ");
for(i=0;i<=9;i++){
for(j=0;j<=14;j++){
printf("%d",word[i][j]);
}
}
printf("\n");
getch();
}
I'm still in the process of learning about matrixes and I've gotten pretty familiar with arrays by now.But the sorting thing is confusing me,this was my attempt but it doesn't work.It lets you write all the words,and then it crashes.
What am I doing wrong here?And how do I correct it?
In your code here:
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
the variables n and f are used uninitialised. That will most likely be the cause of the crash.
f,n are uninitialized. It has garbage and is the reason for crashing at this point.
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n]) // f,n are uninitialized and are error prone
I think this will work..Please excute and tell me..
void main()
{
char word[10][15],temp,sorted_word[15];
int i,j,ii,k,l=0;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;word[i][j]!='\0';j++)
{
ii=i;
for(k=j+1;1;k++)
{
if(ii==9 && word[ii][k]=='\0')
break;
if(word[ii][k]=='\0')
{
ii++;
k=0;
}
if(word[i][j]>word[ii][k])
{
temp=word[i][j];
word[i][j]=word[ii][k];
word[ii][k]=temp;
}
}
sorted_word[l++]=word[i][j];
}
}
sorted_word[l]='\0';
printf("%s",sorted_word);
getch();
}
here the
for(i=0;i<=9;i++)
{ printf("type in wword %d: ",i+1);
gets(word[i]);
}
gets (word[1]);
stores the value from word[1] onwards but where as the character array starts from
word[0].
may be this is not the full solution for u problem
this issue may help u in solving your doubt.
Related
#include<stdio.h>
int main()
{
setbuf(stdout,NULL);
int p,q,r,s,a[p][q],b[r][s],i,j,k,u,v,res[u][v],sum=0;
printf("Enter the number of rows and columns of the 1st matrix: ");
scanf ("%d%d",&p,&q);
printf("Enter the number of rows and columns of the 2nd matrix: ");
scanf ("%d%d",&r,&s);
printf("Enter the elements of matrix1: ");
u=p;
v=s;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix2: ");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<p;i++)
{
for(j=0;j<s;j++)
{
for(k=0;k<r;k++)
{
sum+=a[i][k]*b[k][j];
}
res[i][j]=sum;
sum=0;
}
}
printf("The resultant matrix is: ");
for(i=0;i<p;i++)
{
for(j=0;j<s;j++)
{
printf("%d\t",res[i][j]);
}
printf("\n");
}
return 0;
}
**I'm trying to write a program to perform Matrix Multiplication. The code isn't getting executed...its just terminating and I really cant find the error. When I tried running it online I got ''Bus error(Code Dumped)error 135"...but in my system the programs just terminating without an error. Please help me find the mistake or concept I'm missing here.. **
In the code
int p,q,r,s,a[p][q],b[r][s],i,j,k,u,v,res[u][v],sum=0;
you are using the values of p, q, r, s, u and v uninitialized. As they have automatic storage (local scope) and the type int can have trap representation, and the variables u and v never have their address taken, it'll invoke undefined behaviour. Even for the other variables other than u and v, the values will be indeterminate, resulting in practically invalid code.
To resolve the problem, define the VLAs after you scan the values into the respective variables to be used as array dimension.
I've made an attempt to create the above program,but there's some error.Here's my program
#include <stdio.h>
#include <conio.h>
void main()
{
int a[20],o[20],e[20],c[40],i,j,k,l,n;
printf("enter size of array");
scanf("%d",&n);
printf("enter array");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=(n-j);k++)
{
if(a[i]%2==0)
{
o[j]=a[i];
break;
}
else
{
e[k]=a[i];
}
}
}
}
for(i=1;i<=j;i++)
{
c[l]=o[i];
}
for(i=1;i<=k;i++)
{
c[l+k]=e[k];
}
printf("The new array is");
for(l=1;l<=(j+k);l++)
{
printf(" %d ",c[l]);
}
getch();
}
Can anybody please help me rectify the error in the above program?Can someone also give me a few tips on how to become a good C programmer?
The problem is probably because you never initialize the variable l.
Using an uninitialized variables yields in undefined behaviour (google that).
Also your array indexes start at 1 as for example here:
for (i = 1; i <= n; i++)
In C array indexes start at 0, so you should write this:
for (i = 0; i < n; i++)
This appies to all your other for loops.
There may be other problems though.
Your program looks overly complicated
Using variable names such as odd instead of o and even instead of e would make your program much easier to read.
Your program is poorly formatted. Correct formatting is essential to readbility
So I need to make a staircase but I clearly have something wrong with my logic. Any advice on how to approach this? I only end up getting squares.
#include <stdio.h>
int main()
{
int a,b,z,y,p;
char x;
scanf("%i ", &a);
printf("Number of stairs is: %i\n", a);
printf("up: \n");
for(b=0; b<a; b++) {
for(z=1; a>=z; z++) {
x='x';
p=1;
if ((p=z)) {
printf("%c", x);
}
else {
printf(" ");
}
p++;
}
printf("\n");
}
}
Your code has many flaws and lack of clarity is one of them. Nevertheless, the reason you have a square is because p=z is always true (setting the value of p to z returns the value of z and this, in your code is always 1 or higher - a true value for C standards).
Here is a code that works, loosely based on your example:
#include <stdio.h>
int main() {
int numberSteps,currentStep,currentColumn;
scanf("%i", &numberSteps);
printf("Number of stairs is: %i\n", numberSteps);
printf("up: \n");
for(currentStep=0; currentStep<numberSteps; currentStep++) {
for(currentColumn=0; currentStep>=currentColumn; currentColumn++) {
printf("x");
}
printf("\n");
}
}
Please notice that I changed the variable names so that they became meaningful and also got rid of the unnecessary variables and the unnecessary test if ((p=z)) that was cluttering your code.
Okay I am trying to ask and sort names in C. I completed the code and it compiled without an error but I have a problem. When I input mixed characters that is Uppercase and Lowercase I get Uppercase sorted first and not in order. What should I do to my code ? Please anyone help me.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char name[30][25],temp[25];
int i,j,n;
printf("Enter how many students : ");
scanf("%d",&n);
for(i=0;i<n;i++);
{
printf("Enter the name of the student : ");
scanf("%s",name[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("The sorted names are : \n");
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
getch();
return(0);
}
The loops for your bubble sort are wrong - change:
for(i=0;i<n;i++)
{
for(j=i+j;j<n;j++)
{
to:
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
Alternatively just use qsort from the standard C library rather than trying to re-invent the wheel.
You have two options. One, convert the string to lower (or upper) before comparison. Secondly strcol each string, this puts k next to K etc. Both methods are destructive so you may need to create a work string and free it after comparison.
First of all your program is wrong. For example instead of
scanf("%s",&name[i]);
there has to be
scanf("%s", name[i]);
Or in this statement
for(j=i+j;j<n;j++)
there is used uninitialized variable j.
As for your question then you should convert strings to upper case using standard C function toupper declared in header <ctype.h>
Also it is better to use selection sort without copying strings every time when name[j] is less than name[i].
Multiple errors:
do not end the for loop with a semicolon
for(i=0;i<n;i++);
take a second look at these variables
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
as you described you compare incorrectly for your usecase
if(strcmp(name[i],name[j])>0)
This is a possible solution:
#include <stdio.h>
#include <string.h>
char * Inputs[] =
{
"3\n",
"Carl\n",
"Frank\n",
"carl\n"
};
int in = 0;
int main()
{
char name[30][25],temp[25];
int i,j,n;
printf("Enter how many students : \n");
sscanf(Inputs[in++],"%i",&n);
printf("You entered : %i\n", n);
for(i=0;i<n;i++)
{
printf("Enter the name of the student : \n");
sscanf(Inputs[in++],"%s",&name[i]);
printf("You entered : %s\n", name[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(stricmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("The sorted names are : \n");
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
getch();
return(0);
}
friend i am new in c ,so i face problem in a ,code, plz if there is any wrong in my logic,take it as a pardon eye,
I am trying to find the element in a two dimensional array, so i have declare a two dimensional array in my code, i will take a user input, the input will compare with data in an full array a column index of two dimensional array, if any data found similar of that column index then it will give the same row data of another column of array. if i give a input in my code it is giving output of the number is not in array index, though the number is in the array index, so i dont understand where is my fault.
plz help me to fix the problem.
here is my code :
#include<stdio.h>
int main()
{
int arr[10][3]={{1,5},
{2,8},
{3,27},
{ 4,64},
{5,125},
{6,216},
{ 7,343},
{8,512},
{ 9,729},
{ 10,1000}};
int i, num;
printf("Enter a number\n");
scanf("%d",&num);
for(i=0;i<10;i++)
{
if (num==arr[i][0])
printf("%d",arr[i][1]);
break;
}
if (num==10)
printf("the number is not there");
return 0;
}
You have an errant semi-colon:
if (num==10);
printf("the number is not there");
That call to printf will run each time because there is no body for the if statement. With better formatting:
if (num==10);
printf("the number is not there");
As #zoska points out, you also have the same bug here:
if (num==arr[i][0]);
I would do the following three changes at the minimum:
Change int arr[10][3] to int arr[10][2]
Change
if (num==arr[i][0]);
printf("%d",arr[i][1]);
to
if (num == arr[i][0]) {
printf("%d",arr[i][1]);
}
Change
if (num==10);
printf("the number is not there");
to
if (i == 10) { // note: 'num' changed to 'i'
printf("the number is not there");
}
Your code should look like this in the future
#include <stdio.h>
int main(void)
{
int arr[10][2] = {
{1,5},
{2,8},
{3,27},
{4,64},
{5,125},
{6,216},
{7,343},
{8,512},
{9,729},
{10,1000}
};
int i, num;
printf("Enter a number\n");
scanf("%d", &num);
for(i = 0; i < 10; i++)
{
if (num==arr[i][0]) {
printf("%d", arr[i][1]);
break;
}
}
if (i == 10) {
printf("the number is not there");
}
return 0;
}