Thread: BADLY NEED HELP MASTERS. Thanks!

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    11

    Question BADLY NEED HELP MASTERS. Thanks!

    Here is my problem, I can't figure out this simple tasking job. here it is

    Problem #1
    Ask the user for a number(1-5), say X. Check for input validity. In cases of an invalid input, continuously ask the user to enter a number until he finally enters a valid one. Print the numbers from1 to X for each row starting from row 1 to row X. (It is required to use looping constructs)

    sample run:
    Enter a number (1-5):0
    0 is an invalid entry. Try another.

    Enter a number (1-5): 3

    row 1: 1 2 3
    row 2: 1 2 3
    row 3: 1 2 3

    here's my problematic code for that problem:
    Code:
    #include<stdio.h>
    
    int main (void)
    { int num;
    	do {
    			printf("Enter a number (1-5):");
    			scanf("%d", &num);
    			if((num<1)||(num>5))
    			printf("%d is an invalid entry. Please enter another one.\n", num);
    	}	while ((num<1)||(num>5));
    			for(int i=0; i<1; i++){
    			printf("\nrow%d", i+1);
    			for(int j=0; j<0; j--){
    				printf("%d", j+1);
    			}
    		}		
    	return 0;
    }
    PROBLEM 2:

    write a program that continually asks the user to enter an integer until he/she enters the number 0. Display the maximum of these numbers and the number of times it appeared.

    Sample run:

    Enter a number: 2

    Enter a number: 3

    Enter a number: 5

    Enter a number: 1

    Enter a number: 8

    Enter a number: 8

    The largest number is 8 and it was entered 2 time(s).


    here is my sample problematic program

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	
    	int a, i, m, j, n;
    	
    	printf("enter a number: ");
       scanf("%d", &number1);
       max=count+1;
       count=1;
    
       
    void largest(int a[][MAXCOLS],int m,int n)
     {
      int i,j,largest;
      largest = a[0][0];
      for (i=0;i<m;++i)
      {
       for (j=0;j<n;++j)
       {
        if (a[i][j]>largest)
         largest=a[i][j];
       }
      }
      printf("
    The largest element of the matrix is %d",largest);
    	printf("\n\nThe largest number is %d and it appeared %d time(s).", max, count);
    	
    	
    	
    	
    	
    	
    	
    	return 0;
    }
    by noobprogrammer

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    			for(int i=0; i<1; i++){
    			printf("\nrow%d", i+1);
    			for(int j=0; j<0; j--){
    				printf("%d", j+1);
    			}
    		}
    look at your limits for the for loops...shouldn't they have something to do with num?

    Don't know what you're doing for #2...it should be much shorter than that. All you need to do is keep asking until the input is 0, otherwise the only other thing that should be going on is:
    if the last input is bigger than the largest, set the largest to the last input and reset the number of times the largest has been counted (or increment if you want to keep track, might want to use an array). otherwise, if the input matches the largest, then increment the number of times it's been entered.
    Last edited by Epy; 12-10-2009 at 09:58 AM.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    sufoode, that's using C++, not C, and you shouldn't do his homework for him/her, that's not letting him/her learn.

    Besides, void main doesn't follow the standard, they should be int mains, <stupid mistake>and your solution to #2 doesn't correctly count how many times a number has been entered. What if the user entered 5 5 5 1 5? Your program would say that 5 has been entered once.</stupid mistake>
    Last edited by Epy; 12-10-2009 at 10:17 AM.

  4. #4
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Hell, I went and confused myself. You're right.

    Still, don't do homework for people, and this particular forum section is for C, not C++.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    wow, thanks for replies, maybe the C++ can't help me we are doing it in C our professor forbids using c++ anyway thanks i'll ask you some more questions as i go through your answers and understand it. Thanks by the way. for your replies ^_^

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    I forgot in problem number two after entering 0 the screen will print the largest number and its recurrence in the whole program. Thanks for your help again masters!

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Code:
    	for(int i=0; i<1; i++){
    			printf("\nrow%d", i+1);
    			for(int j=0; j<0; j--){
    				printf("%d", j+1);
    			}
    		}
    Wah! what should I do with the loops? Can someone with a good heart explain to me this please because honestly our professor's mouth is racing with the spped of light. Totally, I'm a beginner in programming.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just put some thought into it!!!! Don't be such a lazy twit.
    Code:
    for(int i=0; i<1; i++){ // How many times will this be executed if i starts at 0 and must be < 1?
         printf("\nrow%d", i+1);
         for(int j=0; j<0; j--){ // How many times will this be executed if j starts at 0 and must be < 0???
    	printf("%d", j+1);
        }
    }

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Code:
    	#include<stdio.h>
    
    int main (void)
    { int num;
    	do {
    			printf("Enter a number (1-5):");
    			scanf("%d", &num);
    			if((num<1)||(num>5))
    			printf("%d is an invalid entry. Please enter another one.\n", num);
    	}	while ((num<1)||(num>5));
    		
    	
    		for(int i=1; i<=num; i++){
    			printf("\nrow %d:", i);	
    		}		
    	return 0;
    }		
    
    ===========
    
    #include<stdio.h>
    
    int main (void)
    { int num;
    	do {
    			printf("Enter a number (1-5):");
    			scanf("%d", &num);
    			if((num<1)||(num>5))
    			printf("%d is an invalid entry. Please enter another one.\n", num);
    	}	while ((num<1)||(num>5));
    			for(int i=0; i<num; i++){
    			printf("\nrow %d: ", i+1);
    			for(int j=0; j<0; j--){
    				printf("%d", j+1);
    			}
    		}		
    	return 0;
    }
    Masters, I've tried this code but all that appears is for example:

    Please enter a number 3:

    row 1:
    row 2:
    row 3:


    what I need is

    row 1: 123
    row 2: 123
    row 3: 123

    Can anyone pinpoint the mistakes in my program? thank you in advance.

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    thanks for the answers, I'm really sorry because I really didn't understand this thing completely.

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Code:
    for(int i=0; i<1; i++){ // How many times will this be executed if i starts at 0 and must be < 1?
         printf("\nrow%d", i+1);
         for(int j=0; j<0; j--){ // How many times will this be executed if j starts at 0 and must be < 0???
    	printf("%d", j+1);
        }
    }
    Am I right the first one will be executed at exactly one time only? while the 2nd one will not be executed. That's wrong in'st it?

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Masters! Thanks for your words of wisdoms and sermons, i've solved the 1st problem, the 2nd one is the only problem. Yahoo!

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Code:
    	#include<stdio.h>
    
    int main (void)
    { int num;
    	do {
    			printf("Enter a number (1-5):");
    			scanf("%d", &num);
    			if((num<1)||(num>5))
    			printf("%d is an invalid entry. Please enter another one.\n", num);
    	}	while ((num<1)||(num>5));
    		
    	
    		for(int i=1; i<=num; i++){
    			printf("\nrow %d:", i);	
    		}		
    	return 0;
    }		
    
    ===========
    
    #include<stdio.h>
    
    int main (void)
    { int num;
    	do {
    			printf("Enter a number (1-5):");
    			scanf("%d", &num);
    			if((num<1)||(num>5))
    			printf("%d is an invalid entry. Please enter another one.\n", num);
    	}	while ((num<1)||(num>5));
    			for(int i=0; i<num; i++){
    			printf("\nrow %d: ", i+1);
    			for(int j=0; j<0; j--){
    				printf("%d", j+1);
    			}
    		}		
    	return 0;
    }
    Masters, I've tried this code but all that appears is for example:

    Please enter a number 3:

    row 1:
    row 2:
    row 3:


    what I need is

    row 1: 123
    row 2: 123
    row 3: 123

    Can anyone pinpoint the mistakes in my program? thank you in advance.

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Look again at this for-loop:

    Code:
    for(int j=0; j<0; j--){
    How many times will the loop be executed?
    Think about how you could rewrite this to make it work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is a Masters in IT Worth it?
    By bengreenwood in forum General Discussions
    Replies: 9
    Last Post: 08-05-2009, 07:10 AM
  2. Masters of Doom
    By medievalelks in forum Game Programming
    Replies: 21
    Last Post: 05-11-2008, 08:21 AM
  3. is a masters degree worth it?
    By Dr Spud in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-25-2007, 03:42 PM
  4. is my loop badly set up?
    By mabufo in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2006, 03:40 PM

Tags for this Thread