make your function as @Ancient Dragon says. and no need to print str1 using for loop in main function.
just use
printf("%s",str1);
make your function as @Ancient Dragon says. and no need to print str1 using for loop in main function.
just use
printf("%s",str1);
well... you just passing a 2D array as a 1D array. then just use this code
int findMin(int* array, int num_rows, int num_cols){
int min = *array; // or can use array[0]
int i;
for( i = 1; i < num_rows * num_cols; i++){
if(min > *(array + i))
min = *(array + i);
}
return min;
}
well.. i could not understand the need of copystring function in your program.
you are using two pointer to char type of variables. when we deal with pointers then this could be done by = operator..
try this one.
#include <stdio.h>
int main(){
char* string1="Hello World!!";
char* string2;
printf("string1: %s\n",string1);
printf("Copying string1 to string2....\n");
string2 = string1;
printf("Now string2:%s\n",string2);
return 0;
}
or if you just want your program to work then try this code
#include <stdio.h>
#include <stdlib.h>
void copyString(char*,char**);
int length(char*);
int main(){
char* string1="Hello world!!";
char* string2;
printf("string1: %s\n",string1);
printf("Copying string1 to string2....\n");
copyString(string1, &string2);
printf("Now string2: %s\n", string2);
return 0;
}
void copyString(char* string1, char** string2){
char *temp = (char*) malloc(sizeof(char) * length(string1));
do{
*temp = *string1;
string1++;
temp ++;
}while( *string1 != '\0');
*temp = '\0';
*string2 = temp;
}
int length(char* string){
int i;
for(i=0;string[i]!='\0';i++);
return i;
}
Well as Narue suggested..
scanf function leaves '\n' into buffer, so when it reaches
while((character=getchar())!= '\n')
reads '\n' from buffer and loop terminates.
after this you are using
puts(sentence);
it will print some garbage value on screen bcoz your string is not terminated by a '\0' char.
to overcome these problems after using scanf function you need to flush input stream and after also terminate your string with '\0'.
Try this:
//Character Manipulation
#include <stdio.h>
void jsw_flush(FILE *in) // Function copied from older post of Narue
{
int ch;
do
ch = getc(in);
while (ch != '\n' && ch != EOF);
clearerr(in);
}
int main()
{
int x;
//for loops
int counter=0;
//for input
char character;
char sentence[20];
printf("Press 1 to use getchar \n Press 2 to use gets \n Press 3 to use sscan \n ");
scanf("%d",&x);
jsw_flush(stdin); // flush input buffer
switch(x)
{
case 1:
printf("Enter something\n");
while((character=getchar())!= '\n')
{
sentence[counter++]=character;
}
sentence[counter] = '\0';
puts(sentence);
break;
}
}
its wrong..
struct LinkedList
{
int num;
struct LinkedList *next;
};
typedef struct LinkedList;
NODE *node,*start,*temp;
you are using
typedef struct LinkedList;
change this line with
typedef struct LinkedList NODE;
Let's have a look at your program...
First thing if you are implementing a function then you need to declare that Function like
short int ** getdat (short int ** ,int );
now main() function use it like
int main()
you are using pointer to a pointer variable m but no declaration also m contains nothing.. it's suppose to store a address of a pointer.. first declare it
int **m;
getdet() function is returning pointer to a pointer type of value and you are directing it to a short int n.. also the way to passing address of m is wrong.
there are few more mistakes in your program.
i suggest first you learn passing pointer to a variable.. and describe your program briefly...
ya a function can return a pointer.
Could you explain what is your program??
@ phobos666
you have just declared a char array
char airfoil[6];
that means it'll store 6 single char including '\0' char.
you want to store 6 string having 3 char that means you need to declare a string array or can say 2 dimension char array.
your program should be like below assuming string length 4 (including '\0') according to your output:
FILE *fp;
char airfoil[6][4];
fp=fopen("/cygdrive/d/airfoil.txt","r");
if(fp!=NULL)
{
while (feof(fp)==0)
{
for(i=0;i<6;i++)
{
fscanf(fp,"%s",airfoil[i]);
}
}
for(i=0;i<6;i++)
{
printf("%s\n",airfoil[i]);
}
}/*end of if*/
fclose(fp);
Exactly what are you trying to do??
@hsetaknev
line 4:
void main()
main always returns a int value so it must be
int main()
line 5:
when you are asking to the user for the value of n then
no need to initialize n=9.
line 13:
could'nt unserstand the need of /0 here even its \0.. no need for it.
line 16:
Its '\n' not '/n'.
Line 19:
place
return 0;
Post your code in proper format..
can you describe your program little more, exactly what are you trying to do??
and always embed your program code between code-tags.
Post your code here showing your effort towards solving this program.
I dint mean that buddy, I want to say this
while ( scanf( "%d", &ch) == 1 )
{
/* on success */
....
....
}
As long as you keep inputing digits (0-9) this loop continues.
>>>if anyone can provide link on some tutorial to use codeblocks..
scanf("%d ",&a[ctr]);
Remove space after %d its causing problem
scanf("%d",&a[ctr]);
@ gaurav
I am also using CODEBLOCKS and i compiled your program and its working fine. its showing sorted output after inputting desired values..
ohh... that's the reason... i forgot it.. thanks :)
I am new to CODEBLOCKS , Recentely switched from older Compilers.
Well I got the solution now..
After allocating Memory for *n its working now..
n = (char *) malloc (20 * sizeof(char));
But I am still a bit confused why did it work on old Turboc compilers.
Plzz Clearfy!!
Thanks.
I am using CODEBLOCKS and I am creating a program that performs basic operations like add, delete and insert on Doubly LinkList , My Program is working fine on older Turboc compilers. But when i run it on codeblock its causing problem when i try to delete a node.
typedef struct dnode
{
struct dnode *prev;
char name[20];
struct dnode *next;
}DNODE;
DNODE *p;
Function that causes Problem is:
char * del_node(int loc)
{
DNODE *temp;
char *n;
int i;
if( !p )
return "\n List is Empty!!";
if(loc >= 1 && loc <= count())
{
temp = p;
if(loc == 1)
p = temp->next;
else
{
for( i = 1 ; i < loc ; i++ , temp = temp->next );
temp->prev->next = temp->next;
}
if(temp->next != NULL )
temp->next->prev = temp->prev;
strcpy( n ,temp->name);
free(temp);
return n;
}
else
return "\n Location not found!!";
}
After Addind some nodes when i tried to delete perticular node it's causing this Error:
DLL.exe has stopped working
Problem signature:
Problem Event Name: APPCRASH
Application Name: DLL.exe
Application Version: 0.0.0.0
Application Timestamp: 4d0a048c
Fault Module Name: msvcrt.dll
Fault Module Version: 7.0.6000.16386
Fault Module Timestamp: 4549bd61
Exception Code: c0000005
Exception Offset: 0001debe
OS Version: 6.0.6000.2.0.0.768.3
Locale ID: 1033
Additional Information 1: 95be
Additional Information 2: 0b3ac16632013f96d01bca0b26ef6e8a
Additional Information 3: ee18
Additional Information 4: e8f12fa66c3490bfb4e8923744a2b7b9
I Figured out that …
you can use
while ( scanf( "%d", &ch) == 1 )
{
....
....
}
The vast majority of functions will give you some indication of success or failure.
Especially with input it's best to make sure that the call succeeded before
proceeding as if it did. In the case of scanf,
a successful call will return the number of items converted:
if (scanf("%d",&ch) == 1)
{
/* Success */
}
else
{
/* Failure */
}
Post your code that you have tried and not working.. and please start new thread for new problem.. after your problem get solved mark your thread as solved..
Embed your program code between code tags..
and use
int main(void)
instead of
void main(void)
Remove getch() from the end
main always return a integer value so use
return 0;
@WaltP sir,
I am actually trying to perform all Arithmatic operations on Large Integer. First i am trying to add two numbers, later i will try to multiply and other operations. that's why i am using polynominals..
I tried to simplyfy my code
here is my structure
typedef struct LargeInt
{
int *coeff;
int max_expo_degree; // it will be one less than the size of Largeint
}LargeInt;
My main
int main(void)
{
char num1[79], num2[79], num3[80];
int i;
LargeInt first, second, third;
// Scaning two large values
printf("\n\n Program Large Integer Arithmatics :- ADDITION");
printf("\n\n Enter 1st Number:");
fscanf( stdin ,"%s", num1 );
printf("\n\n Enter 2nd Number:");
fscanf( stdin ,"%s", num2 );
init(&first , num1); // Initializing first LargeInt
init(&second , num2); // Initializing second
add(first , second , &third); // adding first and second
// converting third LargeInt into String
for( i = 0 ; i <= third.max_expo_degree ; i++ )
num3[third.max_expo_degree - i]= third.coeff[i] + '0';
num3[i] = '\0';
printf("\n Addition of \n %s \n %s \n = %s \n", num1 , num2 , num3);
return 0;
}
Initialisation Function
void init(LargeInt *li, char *num)
{
int i , j , len;
len= strlen(num);
// Allocationg Memory space Equal to length of num string
li->coeff = (int *) malloc((len) * sizeof(int));
for (i=len-1,j=0; i>=0; i--,j++)
li->coeff[j] = num[i] - '0';
li->max_expo_degree = len-1; // one less than size
}
and My Add Function
void add( LargeInt first , LargeInt second , …
The Program is Addition of Large Integers.. Although This program is working But I want to know where i went wrong or what should i need to do to make it better..
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
# include <string.h>
typedef struct poly
{
int *coeff;
int degree;
}poly;
void init(poly *, char *);
void add (poly ,poly ,poly *);
int main(void)
{
char num1[80],num2[80],*sum;
int i;
poly p1,p2,p3;
printf("\n\n Program Large Integer Arithmatics :- ADDITION");
printf("\n\n Enter 1st Number:");
fscanf(stdin,"%s",num1);
printf("\n\n Enter 2nd Number:");
fscanf(stdin,"%s",num2);
init(&p1,num1);
init(&p2,num2);
add(p1,p2,&p3);
for(i = 0 ;i <= p3.degree ; i++)
sum[i] = p3.coeff[i] + 48;
sum[i] = '\0';
printf("\n\t Addition = %s",strrev(sum));
free(p1.coeff);
free(p2.coeff);
free(p3.coeff);
return 0;
}
void init(poly *p, char *num)
{
int i,len,j;
len = strlen(num);
p->coeff = (int *) malloc( len * sizeof(int));
//printf("\n %d",len);
for ( i = len - 1 ,j = 0 ; i >= 0 ; i-- , j++)
p->coeff[j] = num[i] - 48;
p->degree = j - 1;
}
void add(poly p1 , poly p2 , poly *p3)
{
int i , l , carry = 0 , s;
l = p1.degree > p2.degree ? p1.degree : p2.degree;
p3->coeff = (int *) malloc ((l+1) * sizeof(int));
for(i = 0 ; i <= l ;i++)
{
if(i > p1.degree)
s = p2.coeff[i] + carry;
else if (i > p2.degree)
s = p1.coeff[i] + carry;
else
s = p1.coeff[i] + p2.coeff[i] + carry;
carry = 0;
carry = s / 10; …
Why do'nt you use Array instead of using five variables.
like
int num[5];
Declare..
int max, min
Use a loop and scan all values
initialise max and min variable to num[0]
now run a loop and compare all values of array num[5] with both variables..
For that you need a good knowledge of programming, like you need to know worst case complexity, best case complexity and Average time complexity.
Read This
Have'nt you done anything on your own..??
Post your Code here...
well... I was waiting for experts advice, Anyways my problem is already solved so there is no neeed to continue this thread anymore.
@ Shankye Thanks for your suggestions buddy..:)
okk.. what about adding process...
@ shankye
Thanks for replying buddy...
okk.. let me implement what you have suggest then i will reply back..
okk i got it...
void mul(poly p1,poly p2,poly *p3)
{
poly p4,p5;
int i,j,k=0,maxd=0, flag;
for(i=0;i<20;i++)
p5.terms[i].coeff=0;
for(i=0;i<p1.noOfTerms;i++)
{
for(j=0;j<p2.noOfTerms;j++,k++)
{
p4.terms[k].coeff = p1.terms[i].coeff * p2.terms[j].coeff;
p4.terms[k].expo = p1.terms[i].expo + p2.terms[j].expo;
}
}
p4.noOfTerms = k;
for(i=0;i<p4.noOfTerms;i++)
if(p4.terms[i].expo > maxd)
maxd = p4.terms[i].expo;
k=0;
for(i=0;i<=maxd;i++)
{
flag=0;
for(j=0;j<p4.noOfTerms;j++)
{
if(p4.terms[j].expo == i)
{
p5.terms[k].coeff = p5.terms[k].coeff + (p4.terms[j].coeff);
p5.terms[k].expo = p4.terms[j].expo;
flag=1;
}
}
if (flag == 1)
k++;
}
p5.noOfTerms = k;
*p3 = p5;
}
it's working now...
please suggest to make it better...
Let P1 and P2 are two polynominals..
where
p1 = 1 + 4X^2 + x^4
and
p2 = 2x^2 + x^4
program is to multiply p1 with p2
p3 = p1 * p2
Like
line 1: ( 1 + 4x^2 + x^4) (2x^2 + x^4)
line 2: 1*(2x^2 + x^4) + 4x^2 * (2x^2 + x^4) + x^4 * (2x^2 + x^4)
line 3: 2x^2 + x^4 + 8x^4 + 4x^6 + 2x^6 + x^8
line 4: 2x^2 + 9x^4 + 6x^6 + x^8
I am stucked in obtaining line 4..
here is my Polynominal
typedef struct termType
{
int coeff,expo;
}termType;
typedef struct poly
{
termType terms[20];
int noOfTerms;
}poly;
and my multiplication function
void mul(poly p1,poly p2,poly *p3)
{
poly p4;
int i,j,k=0;
for(i=0;i<p1.noOfTerms;i++)
{
for(j=0;j<p2.noOfTerms;j++,k++)
{
p4.terms[k].coeff = p1.terms[i].coeff * p2.terms[j].coeff;
p4.terms[k].expo = p1.terms[i].expo + p2.terms[j].expo;
}
}
p4.noOfTerms = k;
// line 4 code............
}
Any Help Will be Appriciated
Thanks
Vinit Mittal
Try this code..
# include <stdio.h>
# define MAX 100
int main(void)
{
int oddcount, evencount;
int array[MAX],i,n;
printf("\n Enter Array Size (Between 1 to %d ): ",MAX);
scanf("%d", &n); // User enters Array Size
for(i = 0; i < n; i++)
{
printf("\n Enter %d value: ",i+1);
scanf("%d", &array[i]); // Allows user to enter values into the Array
}
putchar('\n');
printf("\nArray Values: ");
for(i = 0; i < n; i++)
{
printf("\n [%d] value is %3d ", i+1, array[i]); //Prints out the array -- this is where program stops it should carry on down
}
//**************************************//
oddcount = 0;
evencount = 0;
for (i = 0; i < n; i++) // Code determines wheter odd or even.
{
if (array[i] % 2 != 0)
{
//printf("\nThe number %d Is Odd.\n", i);
oddcount ++;
}
else
{
//printf("\nThe number %d Is Even.\n",i);
evencount ++;
}
}
printf("\n Odd Num: %d \n Even Num: %d", oddcount , evencount );
return 0;
}
If you are still getting some problem then post your problem here..
If you want to create this function then try this one..
void make_date(int *d,int *m,int *y,int days)
{
int flag=0,temp=0;
// variable days must have positive integer value
while(flag <days)
{
(*d) ++;
if( (*m) == 4 || (*m) == 6 || (*m) == 9 || (*m) == 11)
{
temp=30;
}
else if( (*m) == 2)
{
if( (*y) % 400 == 0 || ( (*y) % 100 != 0 && (*y) % 4 == 0 ))
temp = 29;
else
temp = 28;
}
else
temp=31;
if( (*d) > temp)
{
(*m) ++;
(*d) = 1;
}
if( (*m) == 13)
{
(*y) ++;
(*m) = 1;
}
flag++;
} // end of while
return;
} // end of function
that's good.. :)
Most Welcome Friend.. :)
# include <stdio.h>
# define MAX 100
int main(void)
{
int array[MAX],i,n;
printf("\n Enter Array size (1 - %d):",MAX);
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("\n Enter %d value :",i+1);
scanf("%d", &array[i]);
}
// whatever else you want to do.......
return 1;
}
main function returns a integer vaule.. that is why return 1 is used.
ok
# define MAX 100
int main(void)
{
int array[MAX],n;
// scan array size from user but it must not be greater than MAX
scanf("%d",&n);
// now run your loop from 0 to less than n and scan array values
return 1;
}
Exactly what you want to do... if you just want to print * then it could be done by using a loop. scan a value from the user and run your loop like this..
int i,num;
// input a value here from user in num..
for(i=1;i<=num;i++)
putchar('*');
it's easier to use but cause you are new then you should use predefined arrays.
Its wrong way of passing a variable address to a Pointer
*nextRow = &row; // sets up an arbitrary "first guess"
*nextColumn = &column; // with row, column both = 0
It should be
nextRow = &row;
nextColumn = &column;
if you are not new to C language then you can use malloc or calloc function to create your Arrays..
post your code here. We'll help you to solve your problem.
you can't copy a string into another by using = operator in c, use strcpy function
strcpy (network1.name , network_name);
Post your Program code here...
if you understand Recursion then it's quite easy to understand this program..
return n*n + n*n + n*n + n*m
or
return 2*2 + 3*3 + 4*4 + 5*5
:)
For Removing a file you can use remove macro defined in stdio.h
int remove (const char *filename);
on success it returns 0, but before removing a file be sure that file is closed.
you want to remove argv[1] after copied to argv[2].
if ( remove(argv[1]) == 0)
printf("Removed!");
else
printf("Error Removing File!");
I think it could help you..
hey... please embed your program code between code tags..