statement 20 is unnecessary.
You are anyway setting/ ignoring the value of num in the if else block.
Rashakil Fol commented: seems like a perfectly reasonable question to me. -1
Xufyan commented: thanks...xufyan +1
statement 20 is unnecessary.
You are anyway setting/ ignoring the value of num in the if else block.
You are passing both the array by value (syntax for this is also wrong). Change this to pass by pointer
There are more serious bugs ... Will try to get back to you in some time on those
Google for srand
You want some thing of this sort ...
After you have got random numbers in i and j
if(i==j)
{
// Do nothing
}
else
{
// Execute the loop below
}
for(z=0;z<20;i++)
{
if( (map1[z]==i)&&(map2[z]==j))
{
//Do nothing
}
else
{
// Insert into the array
}
}
There are many more mistakes in your code...
1. When you are accessing the matrix elements the for loops should be
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
// Do printing checking what ever
}
}
2. Initialize count to -1
3. When you are checking for repetition, you have to use == to =
4. I dont think the logic you have used for checking repetition is correct as well. What I would do would be after inputting the number use the loops I have described above to check each entry of the matrix, to see if the input exists there or not
5. Why are you modding the random number by 9 ? I get it that you need 9 entries, but that does not mean all entries have to be smaller than 9. What you want to do is call rand 9 times instead.
Check out this link
http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
In the example code of TCP Client, there is a line
host = gethostbyname("127.0.0.1");
In place of 127.0.0.1 you can use the name of your server
I think we finally have something. Because for small letters (like a b c) it works but for others (like u r k) it just gives -1943. I think this is because of data type. (which is int). u is 21 and 21^13 is really big number so i thought it would be okay if i change types (valuea and b[50]) to double it all becomes 0. If i change them to float then i still got wrong values. Any idea?
#include <stdio.h> #include <string.h> #include <conio.h> char msg[50]; char *p_msg; void encrypt(int b[50], int size) { int index,n,p=43,q=59,e=13; n=p*q; for (index=0;index<size;index++) { /* b[index]=(pow(b[index],e)); printf("Your encrypted message is: \n%.0f\t",fmod(b[index],n)); */ // Use this instead temp= (pow(b[index],e)); printf("Your encrypted message is: %.0f\n",fmod(temp,n)); } } void convert(char a[50]) { int valuea[50]; int index; for (index=0;index<strlen(a);++index) { if (a[index]>='a' && a[index]<='z') { valuea[index]=a[index]-'a'+1; } } for (index=0;index<strlen(msg);++index) { printf("%d\t",valuea[index]); } encrypt(valuea,index); } int main(void) { printf("Enter your message: "); scanf("%s",msg); p_msg=msg; convert(msg); printf("\nYour message was: %s",msg); getch(); return 0; }
Make the correction suggested above ..... I think that will solve your problem
char *letter;
if(info[i].grade >= (mean + 1.5*stddvt)){
info[i].letter= "AA";
This code is wrong. When you say char* letter you just have a pointer. For storing data the pointer has to point to some memory. Check out the function malloc
I dont think
printf("%5s", "*");
can be put in a loop.....
I tried changing the code to:
void AddDevice(int Foo, unsigned int Bar); unsigned long* ptr2data; DWORD temp; *ptr2data = (unsigned long) SomeArrayOfData[4]; temp = (DWORD) ptr2data; AddDevice(7, temp);
as suggested by Jephthah, and it didn't work :(
The method suggested by Deeep wasn't very clear, I tried changing my code to:
void AddDevice(int Foo, unsigned int Bar); unsigned long* ptr2data; DWORD temp; ptr2data = (unsigned long*)&SomeArrayOfData[4]; temp = (DWORD) &ptr2data; AddDevice(7, temp);
this didn't work either :(
Please help me if you have any more suggestions or see what I have done wrong.
Thanks
The variable temp contains the address of the 4th element of the array. When you pass this variable I think you want to pass it this way
AddDevice(7, *temp);
I guess the terminating condition could be when all the fruits are un reachable( height wise) ....
Use this to read a file line by line
char buf[20];
while(accountsinFile.getline(buf,20))
{
cout<<buf<<endl;
}
After this you can parse the string in buf and store its contents in the data members of the struct
Also what are these functions qinit, qremove, qadd ??
Are you sure there are no errors in these functions ?
Right ......
I have given you the logic .....Make the correction on your own
Your program is wrong
This is the O/P of your program
4
5
5
6
6
7
7
7
7
8
8
8
9
9
9
9
9
10
10
10
10
11
11
11
11
11
11
11
11
12
12
12
12
12
13
13
13
13
13
13
13
13
13
13
14
14
14
14
14
14
15
15
15
15
15
15
15
15
15
16
16
16
16
16
16
16
17
17
17
17
17
17
17
17
17
17
17
17
17
17
18
18
18
18
18
18
18
18
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
20
20
20
20
20
20
20
20
20
There is very little difference between those 2 questions .....
Think how can you use my explanation to solve your problem
This is a basic algo for checking if a number is prime or not
Store the number in a
Loop: From i= 2 till i= sqrt(a)+1
if(a%i==0)
break
else
Do nothing
if(i==sqrt(a)+1)
This means that no number between 2 and sqrt(a) is a factor for a. which means a is a prime number
else
a is not a prime number
Now You can extend the above logic to solve your problem
Pattern matching is not that easy with C. Here is one approach though.
If there is some specific pattern to the input then you can check it using if else statements. For example if the input is
!1 \ !2 \ !3
You would say if the first char is a ! then proceed else fail. If you proceeded and then the second char is a digit then proceed.... You get the idea.
How ever if there is no specific pattern as such then you have to be very careful about what you will put in the if else conditions
Hey Thanks...and how to calculate remainder of
a
andb
only ifb
is one less thana
??
i mean when the values ofa
andb
are same then the remainder must not be calculated ?
Put an if condition
if(/*check if b is less than a */)
{
// Calculate the remainder
}
else
{
// Do nothing
}
You are welcome ...
Get started with you HW
Post your progress and a specific question
If you just post the question that you got for an assignment without showing your efforts then you will be ignored
Are you printing on the server like this ?
printf("Calender server has TCP Port %d and IP address %d ", SERVERPORT); // server IP is missing
Then the problem is that printf has just one argument but 2 "%d", so the first %d will print SERVERPORT and the other one will print garbage
You will notice that in the server you have written a while(1). This means that the server can serve an infinite number of requests. If you want to just server any 2 requests then change the while(1) to for(i=0;i<2;i++)
Write the ndice function this way
int Ndice ( void )
{
int nrolls = 0;
while( /*loop infinitely */)
{
printf ("Please enter the numerical amount of dice you would like to roll (maximum 10):\n");
if(/*If the value entered is not correct */
{
// Print the error message
}
else
return nrolls;
}
}
I hope you understood what I am trying to get at
It stores it in argv[1] if you execute the program this way
./client TIME
It stores it in argv[2] if you execute the program this way
./client localhost TIME
Why did you think TIME would be stored in the buff array ?
Thank you a lot.. I already fix the problem in line 66 & here is my new client codes
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <time.h> #define PORT "21708" // the port client will be connecting to #define MAXLEN 100 // max number of bytes we can get at once // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); } return &(((struct sockaddr_in6*)sa)->sin6_addr); } int main(int argc, char *argv[]) { int sockfd, rs, ss; char msg [80]; char buf[MAXLEN]; struct addrinfo hints, *servinfo, *p; int rv; char s[INET6_ADDRSTRLEN]; if (argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); } memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo("nunki.usc.edu", PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and connect to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; } if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("client: connect"); continue; } break; } if (p == NULL) { fprintf(stderr, "client: failed to connect\n"); return 2; } inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); freeaddrinfo(servinfo); // all done with this structure if ((ss = send(sockfd, buf, MAXLEN-1, 0)) == -1) { // send query perror("send"); exit(1); if ((rs = recv(sockfd, msg, MAXLEN-1, 0)) == …
See the objective of the program is to convert from feet to meters. If you use the information that is given in the problem statement ... you see that
10 feet = 12 inches
1 inch= 1/39.37 metres
Therefore
10feet= 12*(1/39.37)metres
Now the program says that accept the input from the user. The user enters the value of feet. This value should be between 10 and 200. So when the user enters the input use a if condition to check if the value is indeed between 10 and 200.
If it is you use a loop to do the conversion. For example, if the user enters 30 feet, this contains 3 10 feets. Initialize a variable to 0. Add 12*(1/39.37) 3 times to the variable.
I hope this will help you to get further in your problem. Implement these points in your code. Read some tutorials on C/C++ programming to better your understanding of input/output, loops.
ALSO NEXT TIME USE CODE TAGS WHEN YOU ARE POSTING CODE
Some thing of this sort if your function is a part of the class definition
class complex
{
int x;
int y;
public :
complex(int x, int y)
{
this->x=x;
this->y=y;
}
void display(complex c)
{
cout<<c.x<<" "<<c.y<<endl;
}
};
int main()
{
complex c1(10,5);
c1.display(c1);
cin.get();
return 0;
}
Put a while loop in the calculate function.... Loop till left over amount has become 0.
One way could be to put a if condition before the mod operation. If c is equal to a, dont do the mod operation. Else go ahead and do the mod operation
First step will be to read from a file. You can use fread for that.
http://irc.essex.ac.uk/www.iota-six.co.uk/c/i2_feof_fgets_fread.asp
Now the 4 bytes that you want to read will be in a character array.
The next part is to assign these bytes to form a 4 byte unsigned number. This code will get you started.... But you will have to modify it to fulfill your specification.
int main()
{
int y=0;
char* ip;
ip=(char*)&y;
*ip=10;
*(ip+1)=11;
*(ip+2)=12; //We are accessing individual bytes of a
// 4 byte integer
*(ip+3)=13;
printf("%u %u %u %u\n",(ip),(ip+1),(ip+2),(ip+3));
printf("%x\n",y);
return 0;
}
In your program when you use malloc, you are allotting space for just one integer, which I think is not what you want to do... If you want to allot space for an array of 5 integers via malloc the correct syntax is
int* ip= (int*)malloc(5* sizeof(int));
Also my suggestion is to type cast irrespective of the fact that you use c or c++ . In that way you will never be wrong
There are 2 big mistakes in your program
1. In the printf that you do on line 66 in the client program is causing a seg fault because of parameter mis match. Comment out that line or put the proper parameters
2. The server is waiting for the client to send Time/ Date. If nothing is received the server just waits without doing anything. But your client does not send that data.
I think you will have to use fgets only.
After you malloc space, you should check if you actually got space or not. That could be one of the reasons for your error. You did not get the requested space.
I ran your code in my system. Using gcc compiler. It compiled and ran fine, but here is another thing you might want to look into.
I gave the input as 10,9,8,7,6 and this is some of the intermediate output that I got. I am printing the values at line 170. As you can see the merge sort adds an extra 0. There is no space allotted for this zero. This also could be causing the error.
Debug Start
9 10
Debug End
Debug Start
8 9 10
Debug End
Debug Start
6 7
Debug End
Debug Start
0 6 7
Debug End
Debug Start
0 6 7 8 9 10
Debug End
In one of my earlier posts I told you to check what function strtok does. The solution to your problem has 2 steps.
1. Use strtok to break the sentence into individual words
2. Check if individual word is a palindrome or not
Checking for palindrome is very simple. The main part of this problem is isolating individual words which strtok will do for you
Have you included math.h ?
Can you post the code where you have actually used the ceil function
Print your array on line 36.... Just before you enter the for loop
What purpose does ch= arr achieve ?
Also when you hit space that means the word has been completed.... So check if the word is symmetrical at that point
On line 32 you have an array s.... where did that come from ?
In line 20, for one of the conditions you have used the = operator instead of the == operator.
= means assignment, where as == means checking for equality. Make the change, compile and run the code. Post what error you are getting
Also you might want to google for the function strtok
I know that I need to edit somewhere in the skipBlanks function.
But I just can't seem to think straight anymore. I was thinking of declaring both \n and ; as characters. then setting them to equal each other. Is that good enough?
HINT: Make a change in line 70
ANOTHER HINT: If I have a number in a variable x, how do I check if the number is not equal to 2 or 3
if(x!=2 && x!=3)
I don't get this error. I mean if i try to free a NULL memory, it does not say anything.
Hey! Look at line# 2 :P
This was another one of my doubts. If i don't type caste calloc/malloc, my compiler is giving me errors. Take a look# include <stdio.h> # include <stdlib.h> int main() { int * element = malloc(sizeof(int)); int * array = calloc(5,sizeof(int)); free(element); free(array); return EXIT_SUCCESS; }
These are the error messages
In function `int main()':| LINE |6| error: invalid conversion from `void*' to `int*'| LINE |7| error: invalid conversion from `void*' to `int*'| ||=== Build finished: 2 errors, 0 warnings ===|
You need to typecast void pointer to int pointer in C++ but in C there is no need to do that. Which compiler did you use to compile this code ?
wow thanks for the cool help! I see what your're saying about subtracting the decimal value of a from the characters so we get like 1, 2, 3 etc for b, c, d, etc.. but I don't get the exact array thing.. like if i do that it'll increment the values inside array and thats all i need to do?
it gives a couple of errors which I'm not exactly sure about :
editedmp3.c: In function 'Count':
editedmp3.c:101: error: expected expression before 'char'http://www.cplusplus.com/reference/clibrary/cstdio/getchar/
editedmp3.c:102: error: expected expression before 'char'
editedmp3.c: In function 'main:
editedmp3.c:139: error: two or more data types in declaration specifiers
editedmp3.c:139: warning: useless type name in empty declarationYour line 139 is int char.....You cannot use char as a name of a variable as it is a keyword. Make it char_1 or something
editedmp3.c:146: error: expected expression before 'char'
The reason for this bug should be apparent noweditedmp3.c:147: error: conflicting types for 'Count'
I guess you wanted to call the function count here . But where as you have declared the function instead.
editedmp3.c:71: error: previous definition of 'Count' was here
editedmp3.c:149: error: expected expression before 'char'
editedmp3.c:149:32: warning: character constant too long for its type
Make the changes that I have suggested. Compile the code and post the results.
PS: Go through some of C programming tutorials
still getting the same thing its adding on or returning a negatvie number -858993460 so its either garbage or the way im filling up the array is wrong or my buble sort
I think your input values are exceeding the maximum allowed value for int in your system. Either change it from int to long. Or reduce the value of the input data
Check the bubble sort. Reduce the input to just 5 numbers. It will be easier for you to debug
Check what is the maximum size of int in your system. I think the input numbers are greater than the maximum allowable. That could be one reason why bubble sort is failing
Change the sequence of steps that you carry out to this.
1. Open the file .
2. Read the data
3. Close the file.
4. Sort the data
5. Open the file
6. Write the data
7. Close the file
Also print the contents of the array num on line 64 to see if you have sorted the data correctly
so should i put the file into an array ie num_array[500] and read the file into the array so i can access num[1] num[5] etc.. and sort them would getline put them in an array though
Yes . But one point of caution. Make sure the size of the file is less than 500. Also you are going into a string but you need is numbers. You will have to use the appropriate conversion methods
right, i remove the ;
can u give me a example how declare in my header the function and how write the code in other file?
plz!
My previous post answered this question. But I guess for your application, you can use your method
Make a header file.... Say file1.h. Include all the header file and function declarations is this file. For ex
#include<stdio.h>
#include<string.h>
void display(int,int);
Then make a .c file. Put all the function declarations in this file. Include the header file, file1.h in the source code file
#include "file1.h"
int display(int a,int b)
{
// Your code here
}
int main()
{
display(1,2);
}
The size of the double pointer .... Not the size of the array