Help with C

Talk about any languages right here. Share and discuss source, but don't expect your homework to be done for you.
Post Reply
salam
n00b
Posts: 2
Joined: Thu Oct 25, 2012 12:25 pm

Help with C

Post by salam » Tue Feb 12, 2013 11:05 pm

pls anyone decode for c program this 2 question. appreciate for help this

QUESTION 1
Write a complete C program to read a single string using the input function gets(). Then test whether the string contains all integers, or all alphabets or a combination of both (i.e., alphanumeric). If the string contains all integers, then print the string as it is. If the string contains all alphabets, then check whether the alphabets are in lowercase or uppercase. If any of the characters in the string is in lowercase, then convert them to uppercase, and vice versa. Finally, print the string. On the other hand, if the string is an alphanumeric, then check whether the alphabet(s) in it is/are in lowercase, then convert it/them to uppercase and vice versa. Finally, print the converted string with the same numeric characters as in the original string. Use the output function puts() whenever you are printing the string.

QUESTION 2

Write a complete C program to declare two one-dimensional arrays, each with 10 integer data items. Then, write a function called readData()¸to read the data items into each array separately. Once the data have been read into each array, you are required to write another function called printLarger(), which prints the contents of an array which is larger than the other array. In other words, you are required to calculate the sum of all the data items in each array, and then compare these sums. If the sum of the first array is larger than the sum of the second array, then you should print the message “array A is larger than array B”, and then print the contents and sum of array A, and vice versa. You can use any names for the arrays as long as they are meaningful. Note that you are required to pass the arrays to the respective functions by reference.
Last edited by Cool_Fire on Wed Feb 13, 2013 3:37 pm, edited 1 time in total.
Reason: Split this post into it's own topic.

User avatar
Cool_Fire
Not a sandwich
Posts: 1912
Joined: Fri May 09, 2003 1:20 pm
Location: 41 6d 73 74 65 72 64 61 6d
Contact:

Re: Help with C

Post by Cool_Fire » Wed Feb 13, 2013 3:38 pm

These aren't very hard problems for any moderately experienced C programmer, but it sounds a lot like we'd be doing your homework for you.
If we're breaking the rules, then how come you can't catch us? You can't find us? I know why. Cause, it's ... MAGIC!
Hackerthreads chat, where the party is going 24/7.

psilocybin
Sargeant at Arms
Posts: 191
Joined: Tue Feb 17, 2009 5:27 am

Re: Help with C

Post by psilocybin » Wed Feb 13, 2013 6:56 pm

I'm not writing any code for you, because like cool fire said this just looks like homework, but I can give you tips. the point of programming languages is to break any problem down into digestible steps, and then digest them. I assume this assignment is to help you get that. I'll format and [comment] the first question so you get the idea.

Write a complete C program to read a single string using the input function gets(). [use gets() in a loop, read documentation on gets() to understand how]

Then test whether the string contains all integers, or all alphabets or a combination of both (i.e., alphanumeric). [make a function for this with meaningful name and return values. you could return an integer and just use constants, or use a typedef enum for the return values (this way is more advanced but probably better. just ignore if you haven't learned about custom type definitions). use a loop inside the function. if you need me to break this down even further I can.]

If the string contains all integers, then print the string as it is.
If the string contains all alphabets, then check whether the alphabets are in lowercase or uppercase.
If any of the characters in the string is in lowercase, then convert them to uppercase, and vice versa.

[you could use a case statement for all of these tests, but it might be more readable to use if-then-else if. personal preference, I like cases.]

Finally, print the string. [I do hope you can manage this on your own :)]


On the other hand, if the string is an alphanumeric,

[this will be inside the scope of an else for the above if statement, or the default case if you used a switch-case structure.]

then check whether the alphabet(s) in it is/are in lowercase, then convert it/them to uppercase and vice versa.

[this is just a simple loop, with an if statement inside. I would use a separate function for readability, which would require you to pass a pointer.]

Finally, print the converted string with the same numeric characters as in the original string. Use the output function puts() whenever you are printing the string.

[just more looping. look up puts() to make sure you know how to use it. separate function.]


hopefully this helps. any time you use a separate function make sure to give it an easily understood name (if your teacher has given you a naming convention use that, otherwise just use names that are obvious about what they do for now), otherwise the readability advantage of using functions for everything is lost, making it all pointless. this program is simple enough that a programmer could probably just understand it anyway if you don't do any of the readability stuff, but it's a good habit to get into.

note that I'm a completely self-taught programmer, so the way I do things might not be how you're supposed to. I made some assumptions about your level of knowledge, so ask away if I said anything confusing if google doesn't help.

salam
n00b
Posts: 2
Joined: Thu Oct 25, 2012 12:25 pm

Re: Help with C

Post by salam » Tue Mar 05, 2013 10:50 pm

for q1 is ok.. finish already. but stuck with q2.. pls assist me to solve this below prblem
Q2
Write a complete C program to declare two one-dimensional arrays, each with 10 integer data items. Then, write a function called readData()¸to read the data items into each array separately. Once the data have been read into each array, you are required to write another function called printLarger(), which prints the contents of an array which is larger than the other array. In other words, you are required to calculate the sum of all the data items in each array, and then compare these sums. If the sum of the first array is larger than the sum of the second array, then you should print the message “array A is larger than array B”, and then print the contents and sum of array A, and vice versa. You can use any names for the arrays as long as they are meaningful. Note that you are required to pass the arrays to the respective functions by reference.

#include<stdio.h>
#define ROWS 10
#define COLS 10



void readData(int [][COLS], int rows, int cols);



int main(void) {
int i, j, a[ROWS][COLS], b[ROWS][COLS], c[ROWS][COLS];
int rows, cols;


printf("Enter number of rows for the Matrix: ");
scanf("%d", &rows);
printf("Enter number of columns for the Matrix: ");
scanf("%d", &cols);

readData(a, rows, cols); /*/ reading matrix a*/
/* readData(b, rows, cols); /* reading matrix b*/



system("pause");
return 0;
}

void readData(int a[][COLS], int rows, int cols) {
int i, j;
printf("Enter the %d elements of the 2-D array row-wise: \n", rows * cols);
for(i=0; i<cols; i++) {
for(j=0; j<rows; j++){
printf("Enter element array%d: ",i+1,j+1);
scanf("%d", &a[j]);
}}
printf("Enter the %d elements of the 3-D array col-wise: \n", rows * cols);
for(i=0; i<cols; i++) {
for(j=0; j<rows; j++){
printf("Enter element array%d: ",i+1,j+1);
scanf("%d", &a[j]);
}}}

User avatar
Cool_Fire
Not a sandwich
Posts: 1912
Joined: Fri May 09, 2003 1:20 pm
Location: 41 6d 73 74 65 72 64 61 6d
Contact:

Re: Help with C

Post by Cool_Fire » Thu Mar 07, 2013 4:16 am

Provided the code you posted there works, you're pretty much done then.
All you'd need to add is a function where you subtract one matrix from the other (just loop trough them and subtract a[0][0] from b[0][0], a[0][1] from b[0][1] etc. If the result is larger than 0 the first matrix is bigger, if the result is smaller than 0, the second matrix is biggest.
If we're breaking the rules, then how come you can't catch us? You can't find us? I know why. Cause, it's ... MAGIC!
Hackerthreads chat, where the party is going 24/7.

Post Reply