Simple programs in C (2)
More simple programs in C owing to the success of the previous C article
1. Write a program that reads a 5 x 5 array of integers and then prints the row sums and the
column sums:
Enter row 1 : 8 3 9 0 10
Enter row 2 : 3 5 17 1 1
Enter row 3 : 2 8 6 23 1
Enter row 4 : 15 7 3 2 9
Enter row 5 : 6 14 2 6 0
Row totals : 30 27 40 36 28
Column totals : 34 37 37 32 21
#include "stdio.h"
int main()
{ int i,j,array[5][5],rowsum[5],colsum[5];
for(i=0;i<5;i++)
{
printf("Enter row %d\t",i+1);
/*Notice the i+1 in previous statement. This is because I started with i=0*/
for(j=0;j<5;j++)
scanf("%d",&array[i][j]);
/*Notice the absence of flower braces for this 'for' loop. This is because there is only one executable statement in this loop*/
}
/*In the code snippet below I am initialising the row and column sum arrays into zero arrays in a concise way.
Try to read carefully and understand. Contact me if you don't understand*/
for(i=0;i<5;rowsum[i]=colsum[i]=0,i++);
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
rowsum[i]=rowsum[i]+array[i][j];
colsum[j]=colsum[j]+array[i][j];
}
printf("\nRow totals :\t");
for(i=0;i<5;i++)
printf("%d\t",rowsum[i]);
printf("\nColumn totals :\t");
for(i=0;i<5;i++)
printf("%d\t",colsum[i]);
}
2. Write a program that reads a message, then prints the reversal of the message:
Enter a message : Don't get mad, get even.
Reversal is : .neve teg ,dam teg t'noD
Hint: Read the message one character at a time (using getchar) and store the characters in an
array. Stop reading when the array is full or the character read is '\n'.
#include "stdio.h"
int main()
{
int i=0,j;
char c,str[50];
printf("Enter a message : ");
while( (( c = getchar() ) != '\n') && (i < 49) )
str[i++] = c;
str[i]='\0';
printf("Reversal is : \t");
for(j=i-1;j>=0;j--)
printf("%c",str[j]);
}
3 Revise the program of question 2 above to use a pointer instead of an integer to keep track of the current position in the array.
#include "stdio.h"
int main()
{
int i=0,j;
char c,str[50],*begin;
begin=str;
printf("Enter a message : ");
while( (( c = getchar() ) != '\n') && (i < 49) )
{
begin = c;
begin++;
i++;
}
begin = '\0';
printf("Reversal is : \t");
for(j=i-1;j>=0;j--)
printf("%c",str[j]);
}
4 Write a program that finds the “smallest” and “largest” in a series of words. After the user enters the words, the program will determine which words would come first and last if the words were listed in dictionary order. The program must stop accepting input when the user enters a four-letter word. Assume that no word is more than 20 letters long. An interactive session with the program might look like this:
Enter word : dog
Enter word : zebra
Enter word : rabbit
Enter word : catfish
Enter word : walrus
Enter word : cat
Enter word : fish
Smallest word : cat
Largest word : zebra
Hint: Use two strings named smallest_word and largest_word to keep track of the “smallest”
and “largest” words entered so far. Each time the user enters a new word, use strcmp to
compare it with smallest_word. If the new word is “smaller”, use strcpy to save it in
smallest_word. Do a similar comparison with largest_word. Use strlen to determine when
the user has entered a four-letter word.
#include "stdio.h"
#include "string.h"
int main()
{ int i=0,flag=1;
char largest_word[20],smallest_word[20],current_word[20];
do{
printf("Enter word : ");
scanf("%s",current_word);
if(strlen(current_word)==4)
break;
else if(i==0)
{ strcpy(largest_word,current_word);
strcpy(smallest_word,current_word);
}
else
{ if(strcmp(largest_word,current_word)<0)
strcpy(largest_word,current_word);
if(strcmp(smallest_word,current_word)>0)
{ flag=0;
strcpy(smallest_word,current_word);
}
}
i=i+1;
}while(1);
printf("Smallest word : %s",smallest_word);
printf("\nLargest word : %s",largest_word);
if(flag==0)
printf("\nWords not in dictionary order");
else
printf("\nWords are in dictionary order");
}
5. Write a program that will read a list of students last names and three integer value scores in the range 0-100. The names and scores should be stored in an array of the following data structures:
* char[] - name
* int[] - three test scores
* double – student average
After reading in the data, the program should calculate the average for each student and
store it in the appropriate field. The program should then sort the array by the students'
last name and then print the last name and average (formatted nicely).
#include "stdio.h"
#include "string.h"
int main()
{
int const NUM_STUD=4;
int i,j;
struct STUD_DATA
{
char name[20];
int score[3];
double avg;
}list[NUM_STUD],temp;
for(i=0;i
{
printf("Enter Last Name of student %d : ",i+1);
scanf("%s",list[i].name);
printf("Enter the three test scores (0-100):\n ");
scanf("%d",&list[i].score[0]);
scanf("%d",&list[i].score[1]);
scanf("%d",&list[i].score[2]);
list[i].avg=(list[i].score[0] + list[i].score[1] + list[i].score[2] )/3;
}
for(i=0;i
for(j=i+1;j
if( strcmp( list[i].name, list[j].name ) > 0 )
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
printf("NAME\t\tSCORE1\tSCORE2\tSCORE3\tAVERAGE\n");
for(i=0;i
{
printf("%s\t\t%d\t%d\t%d\t%2.4f\n",list[i].name,list[i].score[0],list[i].score[1],list[i].score[2],list[i].avg);
}
}
6. Create an application that finds all 4 digit numbers (those would be the numbers in the range 1000-9999) whose value is equal to the sum of each digit raised to the fourth power. MEANINGFUL output should be sent to the screen.
For example, if 1^4 + 2^4 + 3^4 + 4^4 = 1234, then your program would print 1234 as one of the solutions that meets the criteria.
#include "stdio.h"
#include "math.h"
int main()
{
int i,j,d1,d2,d3,d4,sum4;
printf("The numbers identified are :\n");
for(i=1000;i<10000;i++)
{
j=i;
d1=j;
j/=10;
d2=j;
j/=10;
d3=j;
j/=10;
d4=j;
sum4=pow(d1,4)+pow(d2,4)+pow(d3,4)+pow(d4,4);
if(sum4==i)
printf("%d\n",i);
}
}
Nothing Found!
Why not submit your own content? Signup here.
-
What Is Hacking? | By faris_jayz | in Programming
It is not that difficult to hack into a computer system. The most popular definition for hacking is the act of obta...
-
How do I enable JavaScript in browser? | By myjobs | in Programming
way to enable the javascript in different types of browser....
-
How to Set Up a Local Development Environment in Ubuntu | By havard | in Programming
Learn how to set up a local development environment in Ubuntu that set up your websites on the fly....
-
XA Transaction - Solution for Transaction More Than One Database | By H4d1 | in Programming
Have you ever think that it's too difficult for making database transaction in two different places (or databases) ...
-
iPhone Core Data Tutorial Part 3.1 | By eh9212 | in Programming
How to use To-Many relationships in core data...
-
Some simple C programs | By t2thomas | in Programming
Some Simple C programs: feel free to ask questions in the comments section, I will answer as quickly as I see them...
-
Choosing a lawyer | By t2thomas | in Legal
Choosing a good lawyer is a tricky thing. Read these tips to make sure you make the right steps in the unfortunate ...
-
Kerala Style Cabbage Thoran | By t2thomas | in Recipes
If you love vegetarian Indian food, this South-Indian dish will steal your heart. Feel free to modulate the spices ...








No comments yet.